动态更改android通知文本

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)

构造函数正在处理新传递的参数.

Bud*_*ius 9

你无法真正改变通知的内容.这肯定有点烦人,你只需要用新文本替换当前的通知.

我的应用程序有一个上传过程,每隔3秒我们就会更新通知以更改百分比.

所以简单地重新打造的通知,并呼吁notify()NotificationManager用相同的ID.

  • 另外请记住,如果你想要它更新漂亮而不是擦除它并重新创建(这将使你的通知被删除并重新弹出列表中的其他地方),你需要使用`builder.setOnlyAlertOnce(true) )`以及你必须使用相同的构建器,否则它不会更新,它将做愚蠢的娱乐事情.(我已经和它斗争了好几天,我似乎仍然无法更新按钮,它只会添加它们除了现有的:-() (3认同)