我正在尝试创建带有控件的音乐播放器通知。我成功地监听了按钮点击事件,函数也能够正常触发。我所面临的唯一问题是在这些点击事件中更改通知文本。以下是我的尝试:
这个接收器成功接收到调用并完美地触发每一行代码。但我无法更改文本内容。我认为我需要将内容视图重置为通知。如果是这样的话,我该怎么做呢?
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("stop")) {
ABCFragment.stopSong();
Log.d("Notification","Stopping");
}else if (action.equals("play")) {
ABCFragment.togglePlayPause();
Log.d("Notification","Toggle Play/Pause");
RemoteViews contentView = new RemoteViews(context.getPackageName(),R.layout.notification_layout);
contentView.setTextViewText(R.id.songName, "SOME NEW SONG");
}else if (action.equals("next")) {
ABCFragment.playNextSong();
Log.d("Notification","Next");
}
}
解决方案:
我更新了我的通知类构造函数,传递了一个额外的参数,并使其正常工作!
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("stop")) {
ABCFragment.stopSong();
Log.d("Notification","Stopping");
}else if (action.equals("play")) {
ABCFragment.togglePlayPause();
Log.d("Notification","Toggle Play/Pause");
new ABCNotification(context, "SOME NEW SONG");
}else if (action.equals("next")) {
ABCFragment.playNextSong();
Log.d("Notification","Next");
}
}
构造函数处理新传递的参数。
builder.setOnlyAlertOnce(true)
并且必须使用相同的构建器,否则它将不会更新,而是进行怪异的重新创建。 (我已经与此搏斗了几天,但似乎仍无法使按钮更新,它只会添加到现有的按钮上:-() - Jared