2

我正在制作自定义通知,并且在接收者的意图中,我更改了通知本身布局中元素中的文本。当我更新通知时,没有任何反应。意图正在传递给接收者,我签入了日志。发出通知的活动和接收者是不同的类。有任何想法吗?我知道小部件的 AppWidgetProvider 效果很好,但在这种情况下没有 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);
4

1 回答 1

0

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

act.notification.contentView = views;

此代码必须放入接收方代码。必须将更改的 RemoteView 分配给启动通知,然后 Notificationmanager 才能真正用新视图替换旧视图。

于 2014-03-02T10:30:01.350 回答