我正在尝试从耳机或汽车控件接收媒体按钮事件(播放/暂停/等)
这是在我的应用清单中。
<service android:name=".mediaPlayerService.MediaPlayerService"
android:exported="true">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService"/>
</intent-filter>
</service>
<!--
A receiver that will receive media buttons and send as
intents to your MediaBrowserServiceCompat implementation.
Required on pre-Lollipop. More information at
http://developer.android.com/reference/android/support/v4/media/session/MediaButtonReceiver.html
-->
<receiver android:name="android.support.v4.media.session.MediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON"/>
</intent-filter>
</receiver>
这是我的 MediaPlayerService 的一部分
public class MediaPlayerService extends MediaBrowserServiceCompat {
@Override
public void onCreate()
{
super.onCreate();
initMediaSessions();
}
private void initMediaSessions()
{
mSession = new MediaSessionCompat(getApplicationContext(), MediaPlayerService.class.getSimpleName());
setSessionToken(mSession.getSessionToken());
mSession.setCallback(new MediaSessionCompat.Callback()
{
//callback code is here.
}
);
}
@Override
public int onStartCommand(Intent startIntent, int flags, int startId)
{
if (startIntent != null)
{
// Try to handle the intent as a media button event wrapped by MediaButtonReceiver
MediaButtonReceiver.handleIntent(mSession, startIntent);
}
return START_STICKY;
}
好像我错过了什么。当我按下耳机控件上的暂停按钮时,永远不会调用 onStartCommand。
知道为什么这不能按预期工作吗?