gpt4 book ai didi

android - 如何更新Android中NotificationCompat中Action的图标

转载 作者:行者123 更新时间:2023-12-05 05:23:45 26 4
gpt4 key购买 nike

我正在使用 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());

}

最佳答案

要实时更改 Action 图标,您需要:

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

关于android - 如何更新Android中NotificationCompat中Action的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36749370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com