Change Android notification text dynamically

I am trying to make a notification for a music player with controls. I successfully listen to button press events and the functions start correctly. The only problem I encountered is changing the text of the notification about these click events. Here is what I am trying.

This is a receiver that successfully accepts calls and perfectly shoots each line. But I can not change the text. I think I need to reset the contents view for notification. If so, how to do it?

@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"); } } 

Decision:

I updated the constructor of the Notification class to pass additional arguments and got it working!

 @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"); } } 
Constructor

processes the new arguments passed.

+5
source share
1 answer

You cannot change anything in the notification. This is a little annoying, you just need to replace the current notification with new new text.

My application has a download process, and every 3 seconds we update a notification to change the percentage.

just reset the notification and call notify() in the NotificationManager with the same id.

+5
source

Source: https://habr.com/ru/post/1202476/


All Articles