2016-04-05 17 views
-1

Bir proje üzerinde çalışıyorum. Bu projede PDF dosyasını indiriyorum, ancak mükemmel çalışıyorum. İlerleme çubuğu şovu ama problem, 0 (sıfır) 'da takılı olan ilerlemeyi güncellememesi .Ne yapmalıyım?onProgressUpdate() AsyncTask içinde çalışmaz

Sen doInBackground() yönteminde

publishProgress (int ilerleme) çağırmalıdır Kodum

import java.io.File; 
import java.io.IOException; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.ActivityNotFoundException; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Environment; 
import android.util.Log; 
import android.view.View; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    // Progress dialog 
    private ProgressDialog mProgressDialog ; 
    private int mProgressStatus = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     /** Creating a progress dialog window */ 
     mProgressDialog = new ProgressDialog(this); 

     /** Close the dialog window on pressing back button */ 
     mProgressDialog.setCancelable(true); 

     /** Setting a horizontal style progress bar */ 
     mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 

     /** Setting a message for this progress dialog 
     * Use the method setTitle(), for setting a title 
     * for the dialog window 
     * */ 
     mProgressDialog.setMessage("Downloading..."); 


    } 

    public void downloadPDF(View v) { 

     /** Show the progress dialog window */ 
     mProgressDialog.show(); 

     new DownloadFile().execute("https://letuscsolutions.files.wordpress.com/2015/07/five-point-someone-chetan-bhagat_ebook.pdf", "five-point-someone-chetan-bhagat_ebook.pdf"); 

    } 

    public void viewPDF(View v) { 
     File pdfFile = new File(Environment.getExternalStorageDirectory() + "/PDF DOWNLOAD/" + "five-point-someone-chetan-bhagat_ebook.pdf"); // -> filename = maven.pdf 
     Uri path = Uri.fromFile(pdfFile); 
     Intent pdfIntent = new Intent(Intent.ACTION_VIEW); 
     pdfIntent.setDataAndType(path, "application/pdf"); 
     pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     try { 
      startActivity(pdfIntent); 
     } catch (ActivityNotFoundException e) { 
      Toast.makeText(MainActivity.this, "No Application available to view PDF", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    private class DownloadFile extends AsyncTask<String, Integer, Void> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      mProgressStatus = 0; 
     } 

     @Override 
     protected Void doInBackground(String... strings) { 

      publishProgress(mProgressStatus); 

      String fileUrl = strings[0]; // -> https://letuscsolutions.files.wordpress.com/2015/07/five-point-someone-chetan-bhagat_ebook.pdf 
      String fileName = strings[1]; // ->five-point-someone-chetan-bhagat_ebook.pdf 
      String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 
      File folder = new File(extStorageDirectory, "PDF DOWNLOAD"); 
      folder.mkdir(); 


      File pdfFile = new File(folder, fileName); 

      try { 
       pdfFile.createNewFile(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      FileDownloader.downloadFile(fileUrl, pdfFile); 
      return null; 

     } 

     /** This callback method is invoked when publishProgress() 
     * method is called */ 
     @Override 
     protected void onProgressUpdate(Integer... values) { 
      super.onProgressUpdate(values); 
      mProgressDialog.setProgress(mProgressStatus); 
     } 

     @Override 
     protected void onPostExecute(Void aVoid) { 
      super.onPostExecute(aVoid); 
      mProgressDialog.dismiss(); 
      Toast.makeText(getApplicationContext(), "Download PDf successfully", Toast.LENGTH_SHORT).show(); 
      Log.d("Download complete", "----------"); 
     } 

    } 

} 
+0

'mProgressStatus' değiştiremiyorsunuz –

+0

' do'Background' yönteminde 'mProgressStatus' değeri güncellenmiyor –

+0

cevabını gönderebilir misiniz? –

cevap

0

bakınız.

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { 
    protected Long doInBackground(URL... urls) { 
     int count = urls.length; 
     long totalSize = 0; 
     for (int i = 0; i < count; i++) { 
      totalSize += Downloader.downloadFile(urls[i]); 
      publishProgress((int) ((i/(float) count) * 100)); 
      // Escape early if cancel() is called 
      if (isCancelled()) break; 
     } 
     return totalSize; 
    } 

    protected void onProgressUpdate(Integer... progress) { 
     setProgressPercent(progress[0]); 
    } 

    protected void onPostExecute(Long result) { 
     showDialog("Downloaded " + result + " bytes"); 
    } 
} 
+0

Benim durumumdaki ilerlemeyi nasıl hesaplayabilirim? –

1

gibi

Sen yüklemenin ilerleme hesaplanması değildir. mProgressStatus her zaman sıfır olacaktır. İlk önce indirme boyutunu yüzde olarak hesaplayın ve mProgressStatus numaralı telefondan saklayın, ardından publishProgress(mProgressStatus) numaralı telefonu arayın.

İlgili konular