android - 安卓音乐播放器的通知

标签 android notifications audio-player remoteview android-music-player

如何通过通知中的按钮控制音乐播放器,以及如何从通知中收听按钮 Action

最佳答案

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

setOnClickPendingIntent()方法会绑定(bind)一个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:

响应 Action 的服务是这样的:

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
}

在 list 中注册服务:

    <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中复制一些有用的代码 fragment 。 .

关于android - 安卓音乐播放器的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21872022/

相关文章:

android - 如何在 Android Studio 中构建项目测试目录?

java - Android - 引用 DialogInterface.OnClickListener 中的主要可运行对象

android - 在后台 30 分钟后停止 Android 杀死我的 Activity

android - 具有自己的 JSON 和 Youtube 音频库的通用音乐播放器

python - 音乐播放器播放列表

iPhone 允许其他应用程序在录制音频/Shazam 时播放音频

android - 如何将 Observable 对象映射到 Flowable<?> ?

android - 如何允许仅在edittext android的最左边位置插入字符

ios - GCM(Google Cloud Messaging)将通知从python应用服务器推送到iOS应用

android - 我的应用程序无法在 API 23 中运行