Why NotificationManager works so slow during the update progress?

笑着哭i 提交于 2019-12-05 11:32:59

This is a common behavior. You shouldn't flood the NotificationManager with frequent updates. You should decide an interval to update, like twice every second.

For example,

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 perfectly for me and doesn't freeze the notifications too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!