Android音乐播放器的通知

前端之家收集整理的这篇文章主要介绍了Android音乐播放器的通知前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何从通知中的按钮控制音乐播放器,以及如何从通知中收听按钮操作

解决方法

您应该使用RemoteViews的setOnClickPendingIntent()来监听通知中的按钮操作.

setOnClickPendingIntent()方法将绑定PendingIntent以响应按钮单击事件.

示例代码如下:

  1. private void showControllerInNotification() {
  2. PendingIntent pendingIntent = null;
  3. Intent intent = null;
  4.  
  5.  
  6. //Inflate a remote view with a layout which you want to display in the notification bar.
  7. if (mRemoteViews == null) {
  8. mRemoteViews = new RemoteViews(getPackageName(),R.layout.notification_control_bar);
  9. }
  10.  
  11. //Define what you want to do after clicked the button in notification.
  12. //Here we launcher a service by an action named "ACTION_STOP" which will stop the music play.
  13. intent = new Intent(ACTION_STOP);
  14. pendingIntent = PendingIntent.getService(getApplicationContext(),REQUEST_CODE_STOP,intent,PendingIntent.FLAG_UPDATE_CURRENT);
  15.  
  16. //In R.layout.notification_control_bar,there is a button view identified by bar_btn_stop
  17. //We bind a pendingIntent with this button view so when user click the button,it will excute the intent action.
  18. mRemoteViews.setOnClickPendingIntent(R.id.bar_btn_stop,pendingIntent);
  19.  
  20. //Create the notification instance.
  21. mNotification = new NotificationCompat.Builder(getApplicationContext())
  22. .setSmallIcon(R.drawable.ic_launcher).setOngoing(true)
  23. .setWhen(System.currentTimeMillis())
  24. .setContent(mRemoteViews)
  25. .build();
  26.  
  27. //Show the notification in the notification bar.
  28. mNotifiManager.notify(NOTIFICATION_ID,mNotification);
  29. }

更新1:

响应操作的服务是这样的:

  1. public class MediaPlayerService extends Service {
  2.  
  3. public static final String ACTION_STOP="xxx.yyy.zzz.ACTION_STOP";
  4. public static final String ACTION_PLAY="xxx.yyy.zzz.ACTION_PLAY";
  5. public static final String ACTION_PAUSE="xxx.yyy.zzz.ACTION_PAUSE";
  6.  
  7. @Override
  8. public int onStartCommand(Intent intent,int flags,int startId) {
  9. if (intent != null) {
  10. String action = intent.getAction();
  11. if (!TextUtils.isEmpty(action)) {
  12. if (action.equals(ACTION_PLAY)) {
  13. startPlay();
  14. }else if(action.equals(ACTION_PAUSE)) {
  15. pausePlay();
  16. }else if(action.equals(ACTION_STOP)) {
  17. stopPlay();
  18. }
  19. }
  20. }
  21. return super.onStartCommand(intent,flags,startId);
  22. }
  23. private void stopPlay(){
  24. // do the play work here
  25. }
  26. private void stopPause(){
  27. // do the pause work here
  28. }
  29. private void stopPlay(){
  30. // do the stop work here
  31. }

}

在清单中注册服务:

  1. <service
  2. android:name="xxx.yyy.zzz.MusicPlayService"
  3. android:exported="false" >
  4. <intent-filter>
  5. <action android:name="xxx.yyy.zzz.ACTION_PLAY" />
  6. <action android:name="xxx.yyy.zzz.ACTION_PAUSE" />
  7. <action android:name="xxx.yyy.zzz.ACTION_STOP" />
  8. <category android:name="android.intent.category.DEFAULT" />
  9. </intent-filter>
  10. </service>

对于音乐播放控件,您可以从here复制一些有用的代码sinppets.

猜你在找的Android相关文章