Why is NotificationManager so slow during the upgrade?

I am publishing file upload progress through NotificationManager, but when updating its progress, the user interface freezes.

I use NotificationCompat.Builder, which is cached in the class field. So posting progress is very simple:

manager.notify(id, uploader. setProgress(MAX_PROGRESS, (int) (progress * 100), false). build() ); 

Success will be guaranteed to be executed from the main thread (wrapped in a handler decoder).

 this.request.setCallback(new UploaderDecorator(this.request.getCallback())); 

The publication of progress itself is as follows:

 long total = file.length(); long uploaded = 0; int bytesRead = input.read(buffer, 0, bufferSize); while (bytesRead > 0) { output.write(buffer, 0, bufferSize); uploaded += bytesRead; callback.onUploadProgress(activeFile, ((float) uploaded / total)); bytesRead = input.read(buffer, 0, bufferSize); } 

So why does it work so slow?

+4
source share
1 answer

This is the usual behavior. You should not flood NotificationManager with frequent updates. You must select the interval for updating, for example, twice per second.

For instance,

 long startTime; long elapsedTime = 0L; if (elapsedTime > 500) { new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { mBuilder.setProgress(100, (int) newValue, false); mNotifyManager.notify(notificationID, mBuilder.build()); startTime = System.currentTimeMillis(); elapsedTime = 0; } }); Log.d("Andrognito", newValue + "Progress"); } else elapsedTime = new Date().getTime() - startTime; 

This works great for me and does not delay notifications.

+6
source

Source: https://habr.com/ru/post/1495196/


All Articles