我正在服务中创建自定义通知(MusicService)并添加按钮,如下所示。
//Notification
Intent notIntent = new Intent(this, MainActivity.class);
notIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendInt = PendingIntent.getActivity(this, 0,
notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//Play
Intent playIntent = new Intent(this, MusicService.class);
playIntent.setAction(ACTION_PLAY);
PendingIntent pPlayIntent = PendingIntent.getService(this, 1,
playIntent, 0);
//Pause
Intent pauseIntent = new Intent(this, MusicService.class);
pauseIntent.setAction(ACTION_PAUSE);
PendingIntent pPauseIntent = PendingIntent.getService(this, 1,
playIntent, 0);
//Previous
Intent previousSongIntent = new Intent(this, MusicService.class);
previousSongIntent.setAction(ACTION_PREVIOUS);
PendingIntent pPreviousSongIntent = PendingIntent.getService(this, 1,
previousSongIntent, 0);
//Next
Intent nextSongIntent = new Intent(this, MusicService.class);
nextSongIntent.setAction(ACTION_NEXT);
PendingIntent pNextSongIntent = PendingIntent.getService(this, 1,
nextSongIntent,0);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setOngoing(true)
.setContentText(songTitle)
.setContentIntent(pendInt)
.setPriority(Notification.PRIORITY_MAX)
.setWhen(0)
.addAction(R.drawable.ic_play_button, "PLAY", pPlayIntent)
.addAction(R.drawable.ic_pause_button, "PAUSE", pPauseIntent)
.addAction(R.drawable.ic_next_button, "NEXT", pNextSongIntent)
.addAction(R.drawable.ic_previous_button, "PREV", pPreviousSongIntent);
Notification notification = builder.build();
startForeground(NOTIFY_ID, notification);
按钮图标不会显示在通知中。 用两根手指向下滑动通知显示文字(PLAY PAUSE NEXT)。 我错过了什么吗?
答案 0 :(得分:0)
将MediaStyle
与setShowActionsInCompactView
一起使用
builder.setStyle(androidx.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2))