📅  最后修改于: 2023-12-03 15:39:10.634000             🧑  作者: Mango
在Android开发中,通知栏是很重要的一部分。使用NotificationCompat库,我们可以在不同的Android版本间保持兼容性。而使用NotificationCompat.MediaStyle可以扩展通知栏的基本功能。下面是介绍如何使用NotificationCompat.MediaStyle。
在build.gradle文件中添加以下代码:
dependencies {
implementation 'com.android.support:support-media-compat:28.0.0'
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Title")
.setContentText("Content Text")
.setPriority(NotificationCompat.PRIORITY_LOW)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
// add MediaStyle
.setStyle(new MediaStyle()
.setMediaSession(mediaSession.getSessionToken())
.setShowActionsInCompactView(0,1 /* #1: pause button */, 2 /* #2: next button */)
.setShowCancelButton(true)
.setCancelButtonIntent(
MediaButtonReceiver.buildMediaButtonPendingIntent(
this, PlaybackStateCompat.ACTION_STOP)))
.addAction(new NotificationCompat.Action(
R.drawable.ic_previous, "Previous",
MediaButtonReceiver.buildMediaButtonPendingIntent(
this, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)))
.addAction(new NotificationCompat.Action(
R.drawable.ic_pause, "Pause",
MediaButtonReceiver.buildMediaButtonPendingIntent(
this, PlaybackStateCompat.ACTION_PAUSE)))
.addAction(new NotificationCompat.Action(
R.drawable.ic_next, "Next",
MediaButtonReceiver.buildMediaButtonPendingIntent(
this, PlaybackStateCompat.ACTION_SKIP_TO_NEXT)))
.setSubText("Sub Text");
NotificationManagerCompat.from(this).notify(NOTIFICATION_ID, builder.build());
.setStyle(new MediaStyle()
.setMediaSession(mediaSession.getSessionToken())
.setShowActionsInCompactView(0,1 /* #1: pause button */, 2 /* #2: next button */)
.setShowCancelButton(true)
.setCancelButtonIntent(
MediaButtonReceiver.buildMediaButtonPendingIntent(
this, PlaybackStateCompat.ACTION_STOP)))
上述代码中,.setMediaSession()
用于与MediaSessionCompat实例建立连接,并使通知栏与音频播放器同步;.setShowActionsInCompactView()
用于指定在折叠视图中显示哪些动作;.setShowCancelButton()
用于显示取消按钮;.setCancelButtonIntent()
用于指定按下取消按钮后要执行的操作。
.addAction(new NotificationCompat.Action(
R.drawable.ic_previous, "Previous",
MediaButtonReceiver.buildMediaButtonPendingIntent(
this, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)))
.addAction(new NotificationCompat.Action(
R.drawable.ic_pause, "Pause",
MediaButtonReceiver.buildMediaButtonPendingIntent(
this, PlaybackStateCompat.ACTION_PAUSE)))
.addAction(new NotificationCompat.Action(
R.drawable.ic_next, "Next",
MediaButtonReceiver.buildMediaButtonPendingIntent(
this, PlaybackStateCompat.ACTION_SKIP_TO_NEXT)))
上述代码中,.addAction()
用于添加动作按钮。使用MediaButtonReceiver.buildMediaButtonPendingIntent()链接到Action,该方法执行音频播放指令。
上面介绍了如何使用NotificationCompat.MediaStyle扩展Android通知栏的功能。这里给出完整的代码示例。
import android.support.v4.media.app.NotificationCompat;
import android.support.v4.media.session.MediaButtonReceiver;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import android.os.Bundle;
import android.widget.RemoteViews;
public class MainActivity extends AppCompatActivity {
private static final String CHANNEL_ID = "channel_id";
private static final int NOTIFICATION_ID = 0;
private MediaSessionCompat mediaSession;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaSession = new MediaSessionCompat(this, TAG);
mediaSession.setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
}
private void showNotification() {
// create notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Title")
.setContentText("Content Text")
.setPriority(NotificationCompat.PRIORITY_LOW)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
// add MediaStyle
.setStyle(new MediaStyle()
.setMediaSession(mediaSession.getSessionToken())
.setShowActionsInCompactView(0,1 /* #1: pause button */, 2 /* #2: next button */)
.setShowCancelButton(true)
.setCancelButtonIntent(
MediaButtonReceiver.buildMediaButtonPendingIntent(
this, PlaybackStateCompat.ACTION_STOP)))
.addAction(new NotificationCompat.Action(
R.drawable.ic_previous, "Previous",
MediaButtonReceiver.buildMediaButtonPendingIntent(
this, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)))
.addAction(new NotificationCompat.Action(
R.drawable.ic_pause, "Pause",
MediaButtonReceiver.buildMediaButtonPendingIntent(
this, PlaybackStateCompat.ACTION_PAUSE)))
.addAction(new NotificationCompat.Action(
R.drawable.ic_next, "Next",
MediaButtonReceiver.buildMediaButtonPendingIntent(
this, PlaybackStateCompat.ACTION_SKIP_TO_NEXT)))
.setSubText("Sub Text");
NotificationManagerCompat.from(this).notify(NOTIFICATION_ID, builder.build());
}
}
以上即是使用NotificationCompat.MediaStyle扩展通知栏功能的完整教程。