Android 通知中的音乐播放器控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23443946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Music player control in notification
提问by Dhaval Travadi
how to set notification with play/pause, next and previous button in android.!
如何在android中设置播放/暂停、下一个和上一个按钮的通知。!
I am new with Android & also at stack overflow. So please bear with me.
我是 Android 的新手,也是堆栈溢出。所以请耐心等待。
I set notification when song is start to play like below :
我在歌曲开始播放时设置通知,如下所示:
`
`
@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);
}
`
`
I have create BroadcastReceiver like below : `
我已经创建了如下所示的 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 {
}
}
}`
Now, I set custom notification successfully but how can i handle notification buttons and its events like play/pause, previous and next... etc. I also try using broadcast receiver but could not get any response.
现在,我成功设置了自定义通知,但如何处理通知按钮及其事件,如播放/暂停、上一个和下一个...等。我也尝试使用广播接收器,但无法得到任何响应。
Seeking solution and guidance from experts, please help me out.
寻求高手的解决方法和指导,请帮帮我。
Thanks in advance.
提前致谢。
回答by Libin
You need to set a custom intent action
, not the AudioPlayerBroadcastReceiver
component class.
您需要设置一个custom intent action
,而不是AudioPlayerBroadcastReceiver
组件类。
Create a Intent with custom action name like this
创建一个具有自定义操作名称的 Intent,如下所示
Intent switchIntent = new Intent("com.example.app.ACTION_PLAY");
Then, register the PendingIntent
Broadcast
receiver
然后,注册PendingIntent
Broadcast
接收者
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 100, switchIntent, 0);
Then, set a onClick
for the play control , do similar custom action for other controls if required.
然后,onClick
为播放控件设置 a ,如果需要,为其他控件执行类似的自定义操作。
notificationView.setOnClickPendingIntent(R.id.btn_play_pause_in_notification, pendingSwitchIntent);
Next,Register the custom action in AudioPlayerBroadcastReceiver
like this
接下来,AudioPlayerBroadcastReceiver
像这样注册自定义操作
<receiver android:name="com.example.app.AudioPlayerBroadcastReceiver" >
<intent-filter>
<action android:name="com.example.app.ACTION_PLAY" />
</intent-filter>
</receiver>
Finally, when play is clicked on Notification
RemoteViews
layout, you will receive the play action
by the BroadcastReceiver
最后,点击播放时Notification
RemoteViews
的布局,您将收到play action
由BroadcastReceiver
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;
}
}
}
EDIT: how to set the intent filter for Broadcast receiver registered in code
编辑:如何为代码中注册的广播接收器设置意图过滤器
You can also set the Custom Action
through Intent filter
from code for the registered Broadcast receiver
like this
您还可以 像这样 为注册设置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);