I made a mediaplayer app which plays songs in the background with a service. I have a notification which shows the previous, pause/play and next button.
These buttons work but i would like to change my pause/play buttons state in my Activity when i click the pause/play button in my notification.
So when i for example pause the song in my notification, i want it to also change my button in my activity too the pause drawable.
How can i do this?
Code for the notification in my service which handles the buttons state by passing the playBackStatus (Previous, Pause/Play, Next).
private void NotificationBuilder(PlaybackStatus playbackStatus){
int notificationAction = R.drawable.ic_action_pause_white;
PendingIntent play_pause_action = null;
if (playbackStatus == PlaybackStatus.PLAYING){
//Pause button created when song is PLAYING
notificationAction = R.drawable.ic_action_pause_white;
play_pause_action = playbackAction(1);
}else if (playbackStatus == PlaybackStatus.PAUSED){
//Play button created when song is PAUSED
notificationAction = R.drawable.ic_action_play;
play_pause_action = playbackAction(0);
}
EDIT:
play_pause_action is a PendingIntent
notificationBuilder.setShowWhen(false)
.setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle().setMediaSession(mMediaSessionCompat.getSessionToken()).setShowActionsInCompactView(0, 1 ,2))
.setSmallIcon(android.R.drawable.stat_sys_headset).setContentTitle(activeSong.getTitle()).setContentText(activeSong.getArtist())
.addAction(R.drawable.ic_action_prev_white, null, playbackAction(3))
.addAction(notificationAction, null, play_pause_action).addAction(R.drawable.ic_action_next_white, null, playbackAction(2))
.setColor(getResources().getColor(R.color.theme1));
private PendingIntent playbackAction(int actionNumber) {
Intent playbackAction = new Intent(this, MediaPlayerService.class);
switch (actionNumber) {
case 0:
// Play
playbackAction.setAction(Constants.ACTIONS.mACTION_PLAY);
return PendingIntent.getService(this, actionNumber, playbackAction, 0);
case 1:
// Pause
playbackAction.setAction(Constants.ACTIONS.mACTION_PAUSE);
return PendingIntent.getService(this, actionNumber, playbackAction, 0);
case 2:
// Next track
playbackAction.setAction(Constants.ACTIONS.mACTION_NEXT);
return PendingIntent.getService(this, actionNumber, playbackAction, 0);
case 3:
// Previous track
playbackAction.setAction(Constants.ACTIONS.mACTION_PREVIOUS);
return PendingIntent.getService(this, actionNumber, playbackAction, 0);
default:
break;
}
return null;
}
try this :
Button btn=((Button)MainActivity.this.findViewById(R.id.btnPlay));
if (playbackStatus == PlaybackStatus.PLAYING){
//Pause button created when song is PLAYING
btn.setImageResource(R.drawable.pause);
notificationAction = R.drawable.ic_action_pause_white;
play_pause_action = playbackAction(1);
}else if (playbackStatus == PlaybackStatus.PAUSED){
//Play button created when song is PAUSED
btn.setImageResource(R.drawable.play);
notificationAction = R.drawable.ic_action_play;
play_pause_action = playbackAction(0);
}
创建一个界面,然后您就可以处理按钮。在您的界面中添加一个方法,例如示例 handleMybutton (View v){ handle your button what ever you want to do }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.