0

主にWebサービスからファイルをダウンロードしてSDカードに保存することに焦点を当てたアプリケーションを開発しています。クライアントが進行中のダウンロードをキャンセルする必要があるまで、すべてが簡単でした。私は多くの方法を試しましたが、失敗しました。誰でも私を助けて、ダウンロードをキャンセルするためのスニペットを投稿できます。意図的なサービスがはるかに優先されます。

編集:

                    Toast.makeText(ThumbnailView.this, R.string.Download_start, Toast.LENGTH_SHORT).show();
                    pb.setVisibility(View.VISIBLE);
                    info_icon.setVisibility(View.INVISIBLE);
                    langBtn.setVisibility(View.INVISIBLE);
                    name.setVisibility(View.INVISIBLE );
                    author.setVisibility(View.INVISIBLE );
                    lastReading.setVisibility(View.INVISIBLE);
                    intent = new Intent(ThumbnailView.this,
                        DownloadAndExtractFiles.class);
                    Common.isDownloadProgress = true;
                intent.putExtra("BEAN", bean);
                intent.putExtra("FROM", "library");
                intent.putExtra("receiverTag", mReceiver);
                startService(intent);

IntentService クラス:

            try {

                File file = new File(path, /* BOOK_ID */filename + fileformat);
                if (file.exists())
                    file.delete();
                file.createNewFile();
                output = new FileOutputStream(file);

                finalpath = path + "/" + filename + fileformat;

                Log.d("book UNEXTR path", finalpath);
                byte data[] = new byte[1024];
                long total = 0;


                while ((count = input.read(data)) != -1) {
                    if (Common.downloadChkLogout) {
                        // Changed on saturday 9th nov
                        //if (Common.isDownloadProgress) {
                            if (stopped) {
                                break;
                            }
                            total += count;
                            Bundle resultData = new Bundle();
                            resultData.putInt("progress",
                                    (int) (total * 100 / lenghtOfFile));
                            bean.setProgress((int) (total * 100 / lenghtOfFile));
                            rec.send(UPDATE_PROGRESS, resultData);
                            output.write(data, 0, count);
                        }
                    }
                success = true;
                //}
                output.flush();
                output.close();
                input.close();
            } catch (Exception ex) {
                ex.printStackTrace();
                mError = "Download Failed";
                System.out.println("Net Disconnect;:" + mError);

                Toast.makeText(getApplicationContext(), mError,
                        Toast.LENGTH_LONG).show();
            }

編集:

public void onClick(DialogInterface dialog,int id) {
 Log.i("Aftr before service stopped---->>>>>", "true"); 
 Log.i("Intent obj","Intent check..."+intent);
 if(intent!=null)
{ 
 Common.isDownloadProgress = false;
 stopService(intent);

これは私のキャンセルクリックです

サービスを開始するためのコードを投稿し、一部を onHandleIntent にダウンロードしました。よろしくお願いします :)

4

3 に答える 3

0

サービス内から停止しているかどうかを確認する必要があり、その行の半分は完了しています

if (stopped)
    break;

静的ブール値を作成stoppedし、ボタンのクリック時に true に設定します。

編集 あなたはすでにチェックしていますCommon.isDowloadProgressが、コメントされています。次のようにループを破る必要があると思います

while ((count = input.read(data)) != -1)
{
    if (Common.downloadChkLogout)
    {
        if (Common.isDownloadProgress)
        {
            if (stopped)
            {   break;   }
            total += count;
            Bundle resultData = new Bundle();
            resultData.putInt("progress",
                   (int) (total * 100 / lenghtOfFile));
            bean.setProgress((int) (total * 100 / lenghtOfFile));
            rec.send(UPDATE_PROGRESS, resultData);
            output.write(data, 0, count);
        }
        else
        {    break;   }
    }
}
success = true;
于 2013-11-12T10:12:43.820 に答える