2012-07-05 49 views
0

我有一個正在運行的下載功能。但是當我運行它時,如80%的時間讓我的手機變得遲緩,強行關閉,而不是像1〜2分鐘那樣長時間響應。這個案件發生得非常隨機,我真的無法追查到底是什麼問題。下載完成後,設備將恢復正常。我曾嘗試過各種設備,如星系S2,星系筆記,SE xperia Arc S和幾張桌子。問題依然存在。任何人都可以告訴我如何提高我的代碼?下面是我現有的代碼:強制關閉,使用asynctask下載文件

public void onClickDownload(View view){ 
     String url = "http://www.mydomain.com./" + fileURL; 
     url = url.replaceAll(" ","%20"); 
     String sourceUrl = url; 
     new DownloadFileAsync().execute(sourceUrl); 
    } 

public class DownloadFileAsync extends AsyncTask<String, Integer, Void> { 
     private boolean run_do_in_background = true; 

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

    @Override 
    protected Void doInBackground(String... aurl) { 
     int count; 
     try { 
      URL url = new URL(aurl[0]); 
      URLConnection conexion = url.openConnection(); 
      conexion.connect(); 

      int lengthOfFile = conexion.getContentLength(); 
      Log.d("ANDRO_ASYNC", "Lenght of file: " + lengthOfFile); 

      File folder = new File(Environment.getExternalStorageDirectory() + "/MaxApps"); 
      boolean success = false; 
      if (!folder.exists()) { 
       success = folder.mkdirs(); 
      } 
      if (!success) { 
      } else { 
      } 

      InputStream input = new BufferedInputStream(url.openStream()); 
      OutputStream output = new FileOutputStream("/sdcard/MaxApps/" + apkURL); 

      byte data[] = new byte[100*1024]; 

      long total = 0; 

      while ((count = input.read(data)) != -1) { 
      total += count;    
      int progressPercent = (int) ((total*100)/lengthOfFile); 
      if(progressPercent % 5 == 0){ 
       publishProgress(progressPercent); 
       } 
      output.write(data, 0, count); 
      } 

      output.flush(); 
      output.close(); 
      input.close(); 
      } catch (Exception e) { 

       notificationManager.cancel(Integer.parseInt(ID.toString())); 

       Notification MyN = new Notification(); MyN.icon = R.drawable.logo1; 
       MyN.tickerText = "Download Failed"; 
       MyN.number = 1; 
       MyN.setLatestEventInfo (getApplicationContext(), apkURL + " Download Failed.", "Please try again", MyPI); 
       MyN.flags |= Notification.FLAG_AUTO_CANCEL; 
       MyNM.notify(1, MyN);  
       run_do_in_background = false; 
      } 

     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... progress) {   
     notification.contentView.setProgressBar(R.id.pbStatus, 100, progress[0], false); 
     notificationManager.notify(Integer.parseInt(ID.toString()), notification); 
     } 

    @Override 
    protected void onPostExecute(Void unused) { 
     if(run_do_in_background) { 
     notificationManager.cancel(Integer.parseInt(ID.toString())); 

     Notification MyN = new Notification(); MyN.icon = R.drawable.logo1; 
     MyN.tickerText = "Download Complete"; 
     MyN.number = 1; 
     MyN.setLatestEventInfo (getApplicationContext(), "Download Complete, Click to install.", apkURL, MyPI); 
     MyN.flags |= Notification.FLAG_AUTO_CANCEL; 
     MyNM.notify(Integer.parseInt(ID.toString()) , MyN); 
     } 
    } 
} 
+0

我不太喜歡你更新onProgressUpdate中的通知,因爲你在UI線程中執行這些操作,然後你以某種方式阻止它。 – rciovati 2012-07-05 10:00:28

+0

你如何創建並執行'DownloadFileAsync'對象? – Caner 2012-07-05 10:04:53

+0

是的,我確實爲通知欄創建了一個新的用戶界面以滿足我的需要,那我該怎麼辦?這是導致問題的原因嗎? @Caner,檢查更新:) – melvintcs 2012-07-05 10:05:07

回答

0

這可能是更新的UI花了很長時間?也許你可以嘗試測試這樣,看看它是否有差別:

@Override 
protected void onProgressUpdate(Integer... progress) {   
    Log.d("ANDRO_ASYNC", "Progress: " + progress[0]); 
} 

順便說一句,這是不相關的,你問什麼,但我認爲你有一個處理的進展情況在你的代碼的問題:

int progressPercent = (int) ((total*100)/lengthOfFile); 
if(progressPercent % 5 == 0){ 
    publishProgress(progressPercent); 
} 

,只有當它是完全5,10,15,20%等將更新的進展...我猜你真正想要更新的進展時,它至少是5%,進一步比以前。

int previousProgress = 0; 
while ((count = input.read(data)) != -1) { 
    total += count;    
    int progressPercent = (int) ((total*100)/lengthOfFile); 
    if(progressPercent - previousProgress >= 5) { 
     previousProgress = progressPercent; 
     publishProgress(progressPercent); 
    } 
    output.write(data, 0, count); 
} 
+0

感謝您的回覆,但我有一些問題:%5與> = 5之間的差異實際上是什麼?他們只是做同樣的事情嗎?你提供的新計算看起來像是讓系統做更多的工作? – melvintcs 2012-07-05 10:34:00

+0

假設進度是不變的,每次都是%6。所以它會像這樣:6,12,18,24,30,...你的代碼只有在%30%,60%,90%時纔會更新UI。我的建議會每次更新它,是的它是更多的工作。但我只是認爲你的代碼沒有做你打算做的事情。那個建議和你的問題沒有關係.. – Caner 2012-07-05 10:36:49

+0

ic,從來沒有想過它。 thx爲解釋:) – melvintcs 2012-07-05 10:46:10