2012-09-25 89 views
1

我有一個包含進度條的通知。進度條更新代碼運行在一個service.Code低於發佈更改通知進度條

notificationManager.notify(42, notification); 

while((count = inputStream.read(data)) != -1) 
{ 
    //new 
    downloadProgress += count; 
    outputStream.write(data,0,count); 
    //new 
    notification.contentView.setProgressBar(R.id.status_progress, contentLength, downloadProgress, false); 
    notificationManager.notify(42, notification); 
} 

我米麪對是因爲notificatoinManager.notify()方法被稱爲在從流讀出的數據之間的問題,它減慢了複製過程。

我該如何解決這個問題?

+0

我不認爲像這樣會顯着減慢下載過程:P 也許你可以在新的線程更新欄(調用通知方法)... –

+0

是啊我也沒有,直到我執行代碼。 –

回答

3

你也許可以使用的AsyncTask從實際讀數獨立出來UI更新

public class ReadFile extends AsyncTask<Void, Integer, String> { 
@Override 
protected String doInBackground(Void... params) {  
//Do the reading here 
downloadProgress += count;  
outputStream.write(data,0,count); 
publishProgress(downloadProgress); 
} 
@Override 
protected void onProgressUpdate(Integer... progress) { 
notification.contentView.setProgressBar(R.id.status_progress, contentLength, downloadProgress, false);  
notificationManager.notify(42, notification);    
} 

希望這有助於!

+0

我上面發佈的代碼在服務中運行。 –

+0

我同意Royston ......你可以使用AsyncTask,即使它在服務中運行! –

+0

AsyncTask也可以用於服務。 –