android 系统通知播放器,Android音乐播放器的通知

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

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";

@Override

public 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

}

}

在清单中注册服务:

android:name="xxx.yyy.zzz.MusicPlayService"

android:exported="false" >

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值