发布对通知进度条的更改

时间:2012-09-25 11:16:27

标签: android notifications

我有一个包含进度条的通知。 progressBar更新代码在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()方法,这会减慢复制过程。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

您可以使用异步任务将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);               
}

希望这有帮助!