setonClickPendingIntent()方法将绑定PendingIntent以响应按钮单击事件.
示例代码如下:
private voID showControllerInNotification() { PendingIntent pendingIntent = null; Intent intent = null; //Inflate a remote vIEw with a layout which you want to display in the notification bar. if (mRemoteVIEws == null) { mRemoteVIEws = new RemoteVIEws(getPackagename(),R.layout.notification_control_bar); } //define what you want to do after clicked the button in notification. //Here we launcher a service by an action named "ACTION_Stop" which will stop the music play. intent = new Intent(ACTION_Stop); pendingIntent = PendingIntent.getService(getApplicationContext(),REQUEST_CODE_Stop,intent,PendingIntent.FLAG_UPDATE_CURRENT); //In R.layout.notification_control_bar,there is a button vIEw IDentifIEd by bar_btn_stop //We bind a pendingIntent with this button vIEw so when user click the button,it will excute the intent action. mRemoteVIEws.setonClickPendingIntent(R.ID.bar_btn_stop,pendingIntent); //Create the notification instance. mNotification = new NotificationCompat.Builder(getApplicationContext()) .setSmallicon(R.drawable.ic_launcher).setongoing(true) .setWhen(System.currentTimeMillis()) .setContent(mRemoteVIEws) .build(); //Show the notification in the notification bar. mNotifiManager.notify(NOTIFICATION_ID,mNotification); }
更新1:
响应 *** 作的服务是这样的:
public class MediaPlayerService extends Service { public static final String ACTION_Stop="xxx.yyy.zzz.ACTION_Stop"; public static final String ACTION_PLAY="xxx.yyy.zzz.ACTION_PLAY"; public static final String ACTION_PAUSE="xxx.yyy.zzz.ACTION_PAUSE";@OverrIDepublic int onStartCommand(Intent intent,int flags,int startID) { if (intent != null) { String action = intent.getAction(); if (!TextUtils.isEmpty(action)) { if (action.equals(ACTION_PLAY)) { startPlay(); }else if(action.equals(ACTION_PAUSE)) { pausePlay(); }else if(action.equals(ACTION_Stop)) { stopPlay(); } } } return super.onStartCommand(intent,flags,startID);}private voID stopPlay(){ // do the play work here}private voID stopPause(){ // do the pause work here}private voID stopPlay(){ // do the stop work here}
}
在清单中注册服务:
<service androID:name="xxx.yyy.zzz.MusicPlayService" androID:exported="false" > <intent-filter> <action androID:name="xxx.yyy.zzz.ACTION_PLAY" /> <action androID:name="xxx.yyy.zzz.ACTION_PAUSE" /> <action androID:name="xxx.yyy.zzz.ACTION_Stop" /> <category androID:name="androID.intent.category.DEFAulT" /> </intent-filter> </service>
对于音乐播放控件,您可以从here复制一些有用的代码sinppets.
总结以上是内存溢出为你收集整理的Android音乐播放器的通知全部内容,希望文章能够帮你解决Android音乐播放器的通知所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)