如何使RemoteView无效

时间:2014-03-01 15:00:41

标签: android android-notifications android-remoteview

我正在制作自定义通知,并且在接收器中意图我在通知本身的布局中更改元素中的文本。当我更新通知时没有任何反应。意图传递给接收器,我在日志中检查。发出通知的活动和接收者是单独的类。有任何想法吗?我知道AppWidgetProvider for widget工作得很好,但在这种情况下没有AppWidgetProvider,这是明确的通知。有没有其他方法可以使RemoteView无效?

//Receiver.onReceive() method
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.notification_layout);

//make dataString
views.setTextViewText(R.id.n_track_name, dataString);

//while searching i figured out this is something that should update notification
NotificationManager mNotificationManager = (NotificationManager)act.getSystemService(Activity.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, act.notification);

这是在其他活动中第一次进行通知的代码:

Notification notification = new Notification(icon, "Music Player", when);
NotificationManager mNotificationManager = (NotificationManager)MusicPlayerActivity.currentActivity.getSystemService(NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(act.getPackageName(), R.layout.notification_layout);

//make pending intent on button
Intent intent = new Intent(act, NotificationReceiver.class);
intent.setAction(NotificationReceiver.ACTION_NOTIFICATION_NEXT);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(act, 0, intent, 0);
contentView.setOnClickPendingIntent(R.id.n_btnNext, actionPendingIntent);

//make other intents
//show notification
mNotificationManager.notify(1, notification);

1 个答案:

答案 0 :(得分:0)

如果有人有同样的问题,这是解决方案:

act.notification.contentView = views;

此代码必须放入Receiver代码。必须将已更改的RemoteView分配给启动通知,然后只有Notificationmanager才能用new替换旧视图。