簡體   English   中英

單擊通知中的按鈕時如何更新活動 UI?

[英]How to update activity UI when i click a button in my notification?

我制作了一個媒體播放器應用程序,它通過服務在后台播放歌曲。 我有一個通知,顯示上一個、暫停/播放和下一個按鈕。

這些按鈕可以工作,但是當我單擊通知中的暫停/播放按鈕時,我想更改活動中的暫停/播放按鈕狀態。

因此,例如,當我在通知中暫停歌曲時,我希望它也更改我的活動中的按鈕暫停可繪制。

我怎樣才能做到這一點?

我的服務中的通知代碼通過傳遞 playBackStatus(上一個、暫停/播放、下一個)來處理按鈕狀態。

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);

    }

編輯:

play_pause_action 是 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;
    }

嘗試這個 :

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 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM