2012-09-25 44 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也可以用于服务。 –