如何在Android的NotificationCompat中更新操作图标

问题描述 投票:0回答:1

我正在使用MediaPlayer,并且正在使用NotificationCompat.BuilderNotificationManagerCompat进行通知,但是当我播放或暂停音乐时,通知栏中的图标不会改变。我正在使用NotificationCompat MediaStyle。

请建议如何更改通知中的操作图标

更新:

   private void setUpAsForground()
        {Log.e(TAG,"setUpAsForground");
             notificationbuilder
                .setContentIntent(pi);
               notificationbuilder.setSmallIcon(R.drawable.sample_song_icon).setColor(ContextCo     mpat.getColor(this, R.color.colorPrimary));

        notificationbuilder.addAction(new     NotificationCompat.Action(R.drawable.ic_skip_previous_24dp, "prve",
                MediaStyleHelper.getActionIntent(this,    KeyEvent.KEYCODE_MEDIA_PREVIOUS)));

        notificationbuilder.addAction(new NotificationCompat.Action(R.drawable.ic_play_arrow_24dp, "playpause",
                MediaStyleHelper.getActionIntent(this, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE)));
        notificationbuilder.setOngoing(true);



    notificationbuilder.addAction(new NotificationCompat.Action(R.drawable.ic_skip_next_24dp, "next",
            MediaStyleHelper.getActionIntent(this, KeyEvent.KEYCODE_MEDIA_NEXT)));

    notificationbuilder.setStyle(new NotificationCompat.MediaStyle().setShowActionsInCompactView(1).setShowCancelButton(true).
            setCancelButtonIntent(MediaStyleHelper.getActionIntent(this, KeyEvent.KEYCODE_MEDIA_STOP)).setMediaSession(mediaSessionCompat.getSessionToken()));


  startForeground(NOTIFICATION_ID, notificationbuilder.build());

}
    public void updateNotification(String type)
    {

        Log.e(TAG, "updateNotification");
        notificationbuilder.setContentTitle(currentSong.getTitle())
                .setColor(MyApplication.colorAccentLight)
               .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                 .setContentText(currentSong.getArtist());

              if  (mediaSessionCompat.getController().getPlaybackState().getState()==PlaybackStateCompat.STATE_PLAYING )
             {
                 Log.e("mvcdg","play");
               NotificationCompat.getAction(notificationbuilder.build(),1).icon=R.drawable.ic_p    ause_24dp;
        notificationbuilder.setOngoing(true);

             }
        else
             {

                  Log.e("mvcdg", "pause");
                 NotificationCompat.getAction(notificationbuilder.build(),1).icon=R.drawable.ic_p    lay_arrow_24dp;
                notificationbuilder.setOngoing(false);

             }


        try {
          notificationbuilder.setLargeIcon(BitmapFactory.decodeStream(getContentResolver()     .openInputStream(currentSong.getAlbumUriByAlbumId())));
        } catch (FileNotFoundException e) {

        }

    notificationManagerCompat.notify(NOTIFICATION_ID,     notificationbuilder.build());

} 
android android-mediaplayer android-notifications
1个回答
0
投票

要实时更改动作图标,您需要:

  1. 获取并替换操作。
  2. 推送新通知。

例如:

notificationBuilder.actions[1] = new Notification.Action(R.drawable.pause, "play", pendingIntent);
NotificationManager service = ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE));

service.notify(101, notificationBuilder);

notificationBuilder.actions[1] = new Notification.Action(R.drawable.pause, "play", pendingIntent);
startForeground(101, notificationBuilder);

0
投票

解决方案要实时更改操作图标,您需要:

  1. 获取并替换操作。
  2. 推送新通知。

例如:

notificationBuilder.actions[1] = new Notification.Action(R.drawable.pause, "play", pendingIntent);
NotificationManager service = ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE));

service.notify(101, notificationBuilder);

notificationBuilder.actions[1] = new Notification.Action(R.drawable.pause, "play", pendingIntent);
startForeground(101, notificationBuilder);
© www.soinside.com 2019 - 2024. All rights reserved.