Mer*_*ial 6 android android-notifications android-remoteview
我正在尝试通过控件为音乐播放器发出通知.我正在成功地听取按钮单击事件并且正在正常触发功能.我遇到的唯一问题是更改这些点击事件的通知文本.这就是我想要的.
这是接收器成功接收呼叫并完美地触发每一行.但我不能改变文本.我想我必须将内容视图重置为通知.如果是这样,我该怎么做?
@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");
}
}
Run Code Online (Sandbox Code Playgroud)
方案:
我更新了我的Notification类构造函数以传递额外的参数并使其正常工作!
@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");
}
}
Run Code Online (Sandbox Code Playgroud)
构造函数正在处理新传递的参数.
你无法真正改变通知的内容.这肯定有点烦人,你只需要用新文本替换当前的通知.
我的应用程序有一个上传过程,每隔3秒我们就会更新通知以更改百分比.
所以简单地重新打造的通知,并呼吁notify()在NotificationManager用相同的ID.