Android 更改通知的图像时出错
我有个问题。我想更新我的通知,以便在单击时将“暂停”更改为“播放”图像。但我一直在犯这样的错误:Android 更改通知的图像时出错,android,android-notifications,Android,Android Notifications,我有个问题。我想更新我的通知,以便在单击时将“暂停”更改为“播放”图像。但我一直在犯这样的错误: android.app.RemoteServiceException: Bad notification posted from package m.com.musicbox: Couldn't expand RemoteViews for:StatusBarNotification(pkg=m.com.musicbox user=UserHandle{0} id=522 tag=null scor
android.app.RemoteServiceException: Bad notification posted from package m.com.musicbox: Couldn't expand RemoteViews for:StatusBarNotification(pkg=m.com.musicbox user=UserHandle{0} id=522 tag=null score=0: Notification(pri=0 icon=7f020067 contentView=m.com.musicbox/0x7f03003d vibrate=null sound=null defaults=0x0 flags=0x2 when=1416463928948 ledARGB=0x0 contentIntent=Y deleteIntent=N contentTitle=N contentText=N tickerText=N kind=[null]))
对于RemoteView
、NotificationManager
和Notification
,我使用的与更新之前相同的方法。我不确定这是否是正确的方法,因为我仍在尝试找到解决方案。希望有人能帮忙。下面是我的代码:
void updateNotification() {
if (Utils.isStatus.equalsIgnoreCase("pause")) {
notificationView.setImageViewResource(R.id.play, R.drawable.ic_pause);
} else {
notificationView.setImageViewResource(R.id.play, R.drawable.ic_play);
}
notificationManager.notify(id, notification);
}
public void setNotification() {
String ns = Context.NOTIFICATION_SERVICE;
notificationManager = (NotificationManager) getSystemService(ns);
notification = new Notification(R.drawable.ic_launcher, null,
System.currentTimeMillis());
notificationView = new RemoteViews(getPackageName(),
R.layout.notification_bar);
Intent notificationIntent = new Intent(context, Player.class);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
this, 0, notificationIntent, 0);
notification.contentView = notificationView;
notification.contentIntent = pendingNotificationIntent;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
Intent intentPlay = new Intent("m.com.musicbox.ACTION_PLAY");
PendingIntent pendingPlayIntent = PendingIntent.getBroadcast(context,
100, intentPlay, 0);
Intent intentPrev = new Intent("m.com.musicbox.ACTION_PREVIOUS");
PendingIntent pendingPrevIntent = PendingIntent.getBroadcast(context,
100, intentPrev, 0);
Intent intentNext = new Intent("m.com.musicbox.ACTION_NEXT");
PendingIntent pendingNextIntent = PendingIntent.getBroadcast(context,
100, intentNext, 0);
Intent intentClose = new Intent("m.com.musicbox.ACTION_CLOSE");
PendingIntent pendingCloseIntent = PendingIntent.getBroadcast(context,
100, intentClose, 0);
notificationView.setOnClickPendingIntent(R.id.play, pendingPlayIntent);
notificationView.setOnClickPendingIntent(R.id.prev, pendingPrevIntent);
notificationView.setOnClickPendingIntent(R.id.next, pendingNextIntent);
notificationView
.setOnClickPendingIntent(R.id.close, pendingCloseIntent);
notificationManager.notify(id, notification);
}
你应该这样做:
void updateNotification() {
Notification notification;
long when = System.currentTimeMillis();
if (Utils.isStatus.equalsIgnoreCase("pause")) {
notification = new Notification(R.drawable.pause, "String message", when);
} else {
notification = new Notification(R.drawable.play, "String message", when);
}
notificationManager.notify(id, notification);
}
您是否尝试过像这样“notification.icon=R.drawable.icon;”我正在使用自定义布局,并希望在单击图像时更改图像。所以我想我不能使用你的方法,因为我需要原始图像的参考。我更新了关于如何设置通知的代码。