我正在尝试在通知栏上制作一个动态的小图标。它在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)