我一直在使用NotificationCompat.Builder
创建媒体控制通知。当播放状态更改时,我会相应地更新通知。下面的代码片段应该让我了解我一直在做什么。
private NotificationCompat.Builder mNotificationBuilder;
mNotificationBuilder = new NotificationCompat.Builder(this, channelId);
// Prebuild the notification
mNotificationBuilder
.setSmallIcon(R.drawable.ic_statusbar)
.setContentIntent(mSessionActivityIntent)
.setDeleteIntent(closePendingIntent)
.addAction(new NotificationCompat.Action(R.drawable.round_skip_previous_white_36, "Previous", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)))
.addAction(new NotificationCompat.Action(R.drawable.round_play_arrow_white_36, "Play", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)))
.addAction(new NotificationCompat.Action(R.drawable.round_skip_next_white_36, "Next", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_SKIP_TO_NEXT)))
.setShowWhen(false)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_TRANSPORT)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setStyle(new MediaStyle().setShowActionsInCompactView(0, 1, 2)
.setMediaSession(mMediaSessionToken));
当播放状态在播放和暂停之间改变时,我会更新通知以更改播放/暂停按钮。
...
mNotificationBuilder.mActions.set(1, new NotificationCompat.Action(playIcon, "Play", MediaButtonReceiver.buildMediaButtonPendingIntent(this, action)));
...
这之前没有任何问题,现在仍然可以。问题是mActions
字段现在受到限制。我在NotificationCompat.java
来源中发现了这一点。
/** @hide */
@RestrictTo(LIBRARY_GROUP)
public ArrayList<Action> mActions = new ArrayList<>();
很奇怪,现在不提供任何更新方法而将其隐藏的事实。恐怕将来该领域将完全无法访问。有什么想法吗?