2012-06-12 74 views
0

我嘗試將進度條放入通知中。但問題是系統在幾秒鐘內不會響應,進度條一旦啓動就卡在開始處,請注意,即使進度條卡住了,但文件仍在下載。通知中的進度條

下面是代碼,請指導我如何處理進度條。

public class StaffChoice extends Activity { 
    public static final int DIALOG_DOWNLOAD_PROGRESS = 0; 

    ProgressBar progressBar; 
    private int progress = 10; 

    Intent MyI; 
    PendingIntent MyPI; 
    NotificationManager MyNM; 
    Notification notification; 
    NotificationManager notificationManager; 

    private void startDownload() { 
     String url = "http://www.domainurl.com/3d.png";  //this is not a valid url 
     DownloadFileAsync dfa = new DownloadFileAsync(); 
     dfa.setActivity(StaffChoice.this); 
     dfa.execute(url); 
     } 

     public void onTaskCompleted() { 
      dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
     } 

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

     MyI = new Intent(getApplicationContext(), MaxAppsAct.class); 
     MyPI = PendingIntent.getActivity(getApplicationContext(), 0, MyI, 0); 
     MyNM = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 

     Intent intent = new Intent(this, StaffChoice.class); 
     final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); 

     notification = new Notification(R.drawable.logo, "Downloading...", System.currentTimeMillis()); 
     notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT; 
     notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.staffchoices); 
     notification.contentIntent = pendingIntent; 
     notification.contentView.setImageViewResource(R.id.imgIcon, R.drawable.save); 
     notification.contentView.setTextViewText(R.id.tvText, "Downloading..."); 
     notification.contentView.setProgressBar(R.id.pbStatus, 100, progress, false); 

     notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE); 

     notificationManager.notify(42, notification);   
     startDownload(); 
     } 

    public class DownloadFileAsync extends AsyncTask<String, String, String> { 
     StaffChoice activity; 
     private boolean completed; 

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

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

      int lenghtOfFile = conexion.getContentLength(); 
      Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 
      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/3d.png"); 

      byte data[] = new byte[1024]; 
      long total = 0; 

      while ((count = input.read(data)) != -1) { 
      total += count; 
      publishProgress(""+(int)((total*100)/lenghtOfFile)); 
      output.write(data, 0, count); 
      } 

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

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

     @Override 
     protected void onPostExecute(String unused) { 
      completed = true; 
      notifyActivityTaskCompleted(); 
      } 

     public void setActivity(StaffChoice activity){ 
      this.activity = activity; 
      if (completed) { 
       notifyActivityTaskCompleted(); 
       } 
      } 

     private void notifyActivityTaskCompleted() { 
      if (null != activity) { 
       activity.onTaskCompleted(); 
       } 
      } 
     } 
    } 

Logcat:

回答

2

我建議你不應該使用的AsyncTask一個線程內。

要顯示通知,在您的AysnTask,添加以下代碼:

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

更新:在logcat的錯誤看起來像這樣(在解僱對話框原因錯誤): http://blog.doityourselfandroid.com/2010/11/14/handling-progress-dialogs-and-screen-orientation-changes/

可以試試這個:

class YourActivity extends Activity { 

private void startDownload() { 
String url = "http://www.domainurl.com/3d.png"; 
DownloadFileAsync dfa = new DownloadFileAsync(); 
dfa.setActivity(YourActivity.this); 
dfa.execute(url); 
} 

    public void onTaskCompleted() { 
dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
    } 

    public void dismissDialog(int id){ 
    //... 
    } 

    public void showDialog(int id){ 
    //... 
    } 

public class DownloadFileAsync extends AsyncTask<String, String, String> { 

YourActivity activity; 
    private boolean completed; 

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

@Override 
protected String doInBackground(String... aurl) { 
    // nothing change 
} 

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

@Override 
protected void onPostExecute(String unused) { 
     completed = true; 
     notifyActivityTaskCompleted(); 

} 
} 

public void setActivity(YourActivity activity){ 
      this.activity = activity; 
      if (completed) { 
      notifyActivityTaskCompleted(); 
      } 

private void notifyActivityTaskCompleted() { 
if (null != activity) { 
    activity.onTaskCompleted(); 
} 

} 
} 

當您使用publishPro格雷斯doInBackground,應該更新進度通知

+0

好吧,我刪除了線程和實際上做什麼ü說(請參閱更新#1),現在如何運行DownloadFileAsync的類? – melvintcs

+0

@melvintcs:從我的回答中,只需調用new DownloadFileAsync()。execute(url); (把它放到onCreate或按鈕點擊事件同步下載)你應該閱讀AsyncTask的文檔:developer.android.com/reference/android/os/AsyncTask.html – R4j

+0

我已經更新了我的代碼在頂部,看看:) – melvintcs