Android notification progressbar freezing

2019-05-06 16:46发布

This is the code I'm using

http://pastebin.com/3bMCKURu

The problem is that after some time (File gets more weight) notification bar get slower to pulldown, and finally it just freezes!

4条回答
欢心
2楼-- · 2019-05-06 16:59

I ran into a similar problem, it would seem that RemoteViews have a memory leak and shouldn't be reused so much.

Look at these threads:

android memory leak in notification service

http://code.google.com/p/android/issues/detail?id=13941

http://groups.google.com/group/android-developers/browse_thread/thread/667343a171e51463#

Good luck

查看更多
女痞
3楼-- · 2019-05-06 17:06

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. Hope this helps!

查看更多
女痞
4楼-- · 2019-05-06 17:13

Your notifications are too frequent. thats why it freezes. make them update in bigger intervals. Ok is once in every second or 2 seconds.

查看更多
爷、活的狠高调
5楼-- · 2019-05-06 17:14

This solution worked for me (ugly but working):

private static int mPercentDownloaded;

@Override
protected Void doInBackground(String... params) {
...
        mPercentDownloaded = (int) ((total * 100) / lenghtOfFile);
        long currentDownloadTicks = System.currentTimeMillis();
        if (currentDownloadTicks > mDownloadTicks + 1000) {
                publishProgress(mPercentDownloaded);
                mDownloadTicks = currentDownloadTicks;
        }
...
}
查看更多
登录 后发表回答