Note that there are some explanatory texts on larger screens.

plurals
  1. POStop on going download on button click android
    primarykey
    data
    text
    <p>I am developing an application, which focus mainly on downloading a file from web service and storing it in sd card. Everything went easy until my client needs to cancel the on going download. I tried many ways but i failed. So can any one help me out and post some snippets for cancelling on going download. Intent service will be much preferred.</p> <p>Edit: </p> <pre><code> 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); </code></pre> <p>IntentService Class:</p> <pre><code> 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(); } </code></pre> <p>Edit:</p> <pre><code>public void onClick(DialogInterface dialog,int id) { Log.i("Aftr before service stopped----&gt;&gt;&gt;&gt;&gt;", "true"); Log.i("Intent obj","Intent check..."+intent); if(intent!=null) { Common.isDownloadProgress = false; stopService(intent); </code></pre> <p>This is my cancel click</p> <p>I have posted my code for starting the service, and downloading part on onHandleIntent Thanks in advance :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload