Huge memory usage in notifications

前端 未结 4 767
迷失自我
迷失自我 2020-12-28 09:44

I am developing an application with a service which show the progress of a timer in the notification area (with a progress bar and a text). I have extracted below a simpler

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-28 10:34

    I had a similar problem. I had a Service that presented a Notification with a progress bar that corresponded to a file download. The app would crash with a OutOfMemoryError, around ten seconds after the user clicked the Notification bringing them to the app.

    I found that adding .setOngoing(true); to the Builder fixed this problem.

    public NotificationCompat.Builder setOngoing (boolean ongoing)

    Set whether this is an ongoing notification. Ongoing notifications differ from regular notifications in the following ways:

    • Ongoing notifications are sorted above the regular notifications in the notification panel.

    • Ongoing notifications do not have an 'X' close button, and are not affected by the "Clear all" button.

    Example:

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setAutoCancel(true)
                                                             .setDefaults(Notification.DEFAULT_ALL)
                                                             .setContentTitle("Downloading").setContentText("Download in progress...)
                                                                 .setSmallIcon(android.R.drawable.stat_sys_download)
                                                                 .setSound(null)
                                                                 .setDefaults(0)
                                                                 .setOngoing(true);
    

提交回复
热议问题