1

プログレスバーを含む通知があります。プログレスバーの更新コードはサービスで実行されます。コードは以下のとおりです

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()、ストリームからデータを読み取る間にメソッドが呼び出されるためです。コピープロセスが遅くなります。

この問題を解決するにはどうすればよいですか?

4

1 に答える 1

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);               
}

お役に立てれば!

于 2012-09-25T11:24:24.113 に答える