音乐播放器控制通知

如何设置播放/暂停通知,android中的下一个和上一个button。

我是新的Android和堆栈溢出。 所以请忍受我。

在这里输入图像说明

当歌曲开始播放时,我会设置通知如下:

`

@SuppressLint("NewApi") public void setNotification(String songName){ String ns = Context.NOTIFICATION_SERVICE; NotificationManager notificationManager = (NotificationManager) getSystemService(ns); @SuppressWarnings("deprecation") Notification notification = new Notification(R.drawable.god_img, null, System.currentTimeMillis()); RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.notification_mediacontroller); //the intent that is started when the notification is clicked (works) Intent notificationIntent = new Intent(this, AudioBookListActivity.class); PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.contentView = notificationView; notification.contentIntent = pendingNotificationIntent; notification.flags |= Notification.FLAG_NO_CLEAR; //this is the intent that is supposed to be called when the button is clicked Intent switchIntent = new Intent(this, AudioPlayerBroadcastReceiver.class); PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, 0); notificationView.setOnClickPendingIntent(R.id.btn_play_pause_in_notification, pendingSwitchIntent); notificationManager.notify(1, notification); } 

`

我创build了如下所示的BroadcastReceiver:`

  private class AudioPlayerBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); System.out.println("intent action = " + action); long id = intent.getLongExtra("id", -1); if(Constant.PLAY_ALBUM.equals(action)) { //playAlbum(id); } else if(Constant.QUEUE_ALBUM.equals(action)) { //queueAlbum(id); } else if(Constant.PLAY_TRACK.equals(action)) { //playTrack(id); } else if(Constant.QUEUE_TRACK.equals(action)) { //queueTrack(id); } else if(Constant.PLAY_PAUSE_TRACK.equals(action)) { // playPauseTrack(); System.out.println("press play"); } else if(Constant.HIDE_PLAYER.equals(action)) { // hideNotification(); System.out.println("press next"); } else { } } }` 

现在,我成功地设置了自定义通知,但是如何处理通知button及其播放/暂停,上一个和下一个等事件。我也尝试使用广播接收器,但无法获得任何响应。

寻求专家的解决scheme和指导,请帮助我。

提前致谢。

Solutions Collecting From Web of "音乐播放器控制通知"

您需要设置custom intent action ,而不是AudioPlayerBroadcastReceiver组件类。

创build一个像这样的自定义操作名称的意图

  Intent switchIntent = new Intent("com.example.app.ACTION_PLAY"); 

然后,注册PendingIntent Broadcast接收器

  PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 100, switchIntent, 0); 

然后,为播放控件设置一个onClick ,如果需要,可以为其他控件执行类似的自定义操作。

  notificationView.setOnClickPendingIntent(R.id.btn_play_pause_in_notification, pendingSwitchIntent); 

接下来,像这样在AudioPlayerBroadcastReceiver注册自定义动作

  <receiver android:name="com.example.app.AudioPlayerBroadcastReceiver" > <intent-filter> <action android:name="com.example.app.ACTION_PLAY" /> </intent-filter> </receiver> 

最后,当单击Notification RemoteViews布局时,您将收到BroadcastReceiverplay action

 public class AudioPlayerBroadcastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(action.equalsIgnoreCase("com.example.app.ACTION_PLAY")){ // do your stuff to play action; } } } 

编辑: 如何设置代码中注册的广播接收器的意图filter

您也可以像这样通过注册Broadcast receiver代码设置Custom Action通过Intent filter Broadcast receiver

  // instance of custom broadcast receiver CustomReceiver broadcastReceiver = new CustomReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addCategory(Intent.CATEGORY_DEFAULT); // set the custom action intentFilter.addAction("com.example.app.ACTION_PLAY"); // register the receiver registerReceiver(broadcastReceiver, intentFilter);