我使用本指南MediaButtonReceiver
为我的媒体播放器应用程序添加功能。
到目前为止,应用程序处于前台运行良好,但一旦应用程序进入后台,它就会停止接收媒体按钮操作。
部分清单
<service
android:name=".playback.PlayerService"
android:exported="true">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService"/>
</intent-filter>
</service>
<receiver android:name="android.support.v4.media.session.MediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON"/>
</intent-filter>
</receiver>
媒体播放器服务的一部分
@Override
public void onCreate() {
Timber.d("onCreate: ");
super.onCreate();
startService(new Intent(getApplicationContext(), PlayerService.class));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Timber.d("onStartCommand: ");
sessionCompat = new MediaSessionCompat(this, TAG);
setControllerCompat(new MediaControllerCompat(getApplicationContext(), sessionCompat));
sessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS |
MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS);
sessionCompat.setCallback(sessionCallback);
setSessionToken(sessionCompat.getSessionToken());
sessionCompat.setShuffleMode(playerHelper.getShuffleMode());
sessionCompat.setRepeatMode(playerHelper.getRepeatMode());
MediaButtonReceiver.handleIntent(sessionCompat, intent);
return START_NOT_STICKY;
}
请问我做错了什么,当应用程序处于前台时,如何使服务接收按钮操作?