Capture media button on Android >=4.0 (works on 2.3)

别等时光非礼了梦想. 提交于 2019-11-26 22:23:20

问题


I wrote some service which uses BroadcastReceiver to capture one of media buttons ("play button" from a headset), and it works perfectly on android 2.3.x (HTC Nexus One or HTC Desire)

When I tried to run in on Android 4.0.3 (Samsung Nexus S) it doesn't work (my application doesn't receive intent "android.intent.action.MEDIA_BUTTON" and "play" button behaves as usual: stops/starts music).

Content of manifest:

...
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <receiver android:name=".buttonreceiver.MediaButtonIntentReceiver" >
        <intent-filter android:priority="10000" >
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>
...

Is there way to make it work on android 4.0.3


edit: I've try proposed solution, I've added action and run it, but my receiver still doesn't receive intent. What is more strange registering receiver by code also doesn't work:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.about_and_activation_view);

    Log.d("MR", "onCreate - " + getIntent().getAction());

    mReceiver = new MediaButtonIntentReceiver();
    registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_MEDIA_BUTTON));
}

Now I'm totally confused.


回答1:


Make sure that you have an activity in your app, and that the user runs this activity before attempting to press that button. Until then, your <receiver> will not receive any broadcasts.


UPDATE

On Android 4.0 and higher, it appears that you also need to call registerMediaButtonEventReceiver() on AudioManager in order to receive the events. That state will hold until something else calls registerMediaButtonEventReceiver() or until you call unregisterMediaButtonEventReceiver().

For example, an activity like this:

public class MediaButtonActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ((AudioManager)getSystemService(AUDIO_SERVICE)).registerMediaButtonEventReceiver(new ComponentName(
                                                                                                       this,
                                                                                                       MediaButtonReceiver.class));
  }
}

will enable a manifest-registered MediaButtonReceiver to get ACTION_MEDIA_BUTTON events.




回答2:


If you just want your app to be the default but don't need to do anything with the button presses you can use the following method.

Add this to the manifest file (in the "Application" node):

    <receiver android:name="BroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
</receiver>

Add this to onCreate() in the main activity or anywhere you want that is called when the app is run. Could be useful in the onResume() event too:

    mAudioManager =  (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
    mRemoteControlResponder = new ComponentName(getActivity().getPackageName(), BroadcastReceiver.class.getCanonicalName());

    mAudioManager.registerMediaButtonEventReceiver(mRemoteControlResponder);


来源:https://stackoverflow.com/questions/10537184/capture-media-button-on-android-4-0-works-on-2-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!