我正在尝试创建一个使用媒体按钮操作的通知,但是如何发送和接收这些按钮?这是我到目前为止所拥有的...
if(mediaSession==null) {
mediaSession = new MediaSession(this, "Boom");
}
//Album art
ImageView img = (ImageView)findViewById(R.id.img_AlbumArt1);
Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
PendingIntent mPauseIntent = PendingIntent.getBroadcast(this, 0,
new Intent(ACTION_PAUSE).setPackage(this.getPackageName()),
PendingIntent.FLAG_CANCEL_CURRENT);
Notification.Builder mBuilder =
new Notification.Builder(this)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_media_play)
.setLargeIcon(bitmap)
.setContentTitle(playingTitle)
.setContentText(playingArtist)
.setStyle(new Notification.MediaStyle()
.setMediaSession(mediaSession.getSessionToken())
.setShowActionsInCompactView(0,1,2)
)
// Add media control buttons that invoke intents in your media service
.addAction(R.drawable.ic_media_rew, "Previous", null) //#0
.addAction(R.drawable.ic_media_pause, "Play/Pause", null) //#1
.addAction(R.drawable.ic_media_ff, "Next", null) //#2 nextPendingIntent
;
// Creates an explicit intent for an Activity in your app
Intent intent = new Intent(this, Music.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_MUSIC);
// Creates an explicit intent for an Activity in your app
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,PendingIntent.FLAG_CANCEL_CURRENT,intent,0);
mBuilder.setContentIntent(pendingIntent);
NotificationManager NotiMan=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
NotiMan.notify(notification, mBuilder.build());
Android docs建议一个
pendingIntent
,其中我已临时设置了null
,但我一直无法弄清楚如何为此目的制作PendingIntent
。BroadcastReceiver:
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final CharSequence action = intent.getAction();
if(action==ACTION_PAUSE) {
//mTransportControls.pause();
Log.i("ON RECEIVE", "ACTION_PAUSE");
}
}
};
并在onCreate中:
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_PAUSE);
this.registerReceiver(mMessageReceiver, filter);
最佳答案
PendingIntent
需要指向BroadcastReceiver
或可以对指定请求起作用的服务。例如,使用BroadcastReceiver
时,您将有一个待处理的意图,看起来像
public static final String ACTION_PAUSE = "com.pkg.name.ACTION_PAUSE";
mPauseIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE,
new Intent(ACTION_PAUSE).setPackage(pkg),
PendingIntent.FLAG_CANCEL_CURRENT);
接下来,注册动作/待定意图
.addAction(R.drawable.ic_media_pause, mContext.getString(R.string.label_previous), mPendingIntent);
最终,当未决的Intent被触发时,您将处理操作。在这里,您可以选择使用
MediaController.TransportControls
播放/暂停动作。 @Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
switch (action) {
case ACTION_PAUSE:
mTransportControls.pause();
break;
...
}
MediaSession
应该被实例化并设置为对播放/暂停+其他动作起作用。mSession = new MediaSession(this, "Service");
mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
最后扩展
MediaSession.MediaSessionCallback
并实现onPlay()/onPause()
..,这实际上将在回放中起作用。