通知为Android音乐播放器 [英] Notification for android music player

查看:466
本文介绍了通知为Android音乐播放器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从按钮通知控制音乐播放器,以及如何听从通知按钮动作

How to control a music player from buttons in notification, and how to listen button action from the notification

推荐答案

您应该使用RemoteViews的setOnClickPendingIntent()听从通知按钮动作。

You should use setOnClickPendingIntent() of RemoteViews to listen button action from the notification.

该方法setOnClickPendingIntent()将一个PendingIntent绑定到响应按钮单击事件。

The method setOnClickPendingIntent() will bind a PendingIntent to response the button click event.

样品code是这样的:

The sample code is like this:

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:

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

update 1:

The service which responses the action is like this:

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
}

}

注册的服务清单:

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

有关的音乐播放器,你可以从这里复制一些有用的code sinppets

For the music play control,you can copy some useful code sinppets from here.

这篇关于通知为Android音乐播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆