Неустранимое исключение при использовании setSmallIcon (Icon i) в Android 6.0.1 - PullRequest
0 голосов
/ 07 марта 2019

Я пытаюсь сделать динамический маленький значок на панели уведомлений.И это хорошо работало на Android 8.0.0.Но есть проблема на Android 6.0.1.

Вот код:

Intent notificationIntent = new Intent(getApplicationContext(), ResumeMainActivity.class);
            intent.putExtra("type", "exit");
            PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
                    0, notificationIntent, 0);
            Bundle a = new Bundle();
            a.putString("type", "exit");

            Intent snoozeIntent = new Intent(getApplicationContext(), Foreground.class);
            snoozeIntent.putExtra(EXTRA_NOTIFICATION_ID, 0);
            snoozeIntent.setAction("Stop");
            PendingIntent snoozePendingIntent =
                    PendingIntent.getService(getApplicationContext(), 0, snoozeIntent, 0);


            Notification.Builder notification = null;
            NotificationCompat.Builder notificationBelow = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                notification = new Notification.Builder(getApplicationContext())
                        .setContentTitle("Title")
                        .setContentText("Tap to open app.")
                        .setSmallIcon(Icon.createWithBitmap(drawText(totalDataSent, totalDataCollected)))

                        .setContentIntent(pendingIntent)
                        .setExtras(a);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    notification.setChannelId(CHANNEL_ID);
                }
                notification.addAction(new Notification.Action.Builder(Icon.createWithResource(getApplicationContext(), R.drawable.ic_launcher_foreground), "Stop", snoozePendingIntent).build());
            } else {
                notificationBelow = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
                        .setContentTitle("Title")
                        .setContentText("Tap to open app.")
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setContentIntent(pendingIntent)
                        .setExtras(a)
                        .setChannelId(CHANNEL_ID)
                        .addAction(R.drawable.ic_launcher_foreground, "Stop", snoozePendingIntent);
            }
            NotificationManager manager = (NotificationManager)
                    context.getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                manager.notify(1, notification.build());
            } else {
                manager.notify(1, notificationBelow.build());
            }

И я получаю Couldn't update icon: StatusBarIcon(icon=Icon(typ=RESOURCE pkg=com.example.app id=0x00000000) visible user=0 )

Вот ошибка:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.app, PID: 20774
android.app.RemoteServiceException: Bad notification posted from package com.example.app: Couldn't update icon: StatusBarIcon(icon=Icon(typ=RESOURCE pkg=com.example.app id=0x00000000) visible user=0 )
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2019)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7224)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
...