在Android >=4.0上捕获媒体按钮(适用于2.3)

时间:2023-01-17 14:16:30

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)

我写了一些服务,使用BroadcastReceiver来获取一个媒体按钮(耳机上的“play button”),它在android 2.3上运行得很好。x (HTC Nexus One或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).

当我试着在Android 4.0.3(三星Nexus S)上运行时,它不起作用(我的应用程序没有收到“Android .intent. int .action. media_button”和“play”按钮像往常一样:停止/启动音乐)。

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

有没有办法让它在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.

现在我完全糊涂了。

2 个解决方案

#1


13  

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().

在Android 4.0或更高版本中,您似乎还需要在AudioManager上调用registerMediaButtonEventReceiver()来接收事件。这个状态将保持到调用registerMediaButtonEventReceiver()或调用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.

将启用显式注册的MediaButtonReceiver来获取ACTION_MEDIA_BUTTON事件。

#2


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:

将这个添加到onCreate()中,在主活动中,或者在运行应用程序时调用的任何地方。在onResume()事件中也可能有用:

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

    mAudioManager.registerMediaButtonEventReceiver(mRemoteControlResponder);

#1


13  

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().

在Android 4.0或更高版本中,您似乎还需要在AudioManager上调用registerMediaButtonEventReceiver()来接收事件。这个状态将保持到调用registerMediaButtonEventReceiver()或调用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.

将启用显式注册的MediaButtonReceiver来获取ACTION_MEDIA_BUTTON事件。

#2


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:

将这个添加到onCreate()中,在主活动中,或者在运行应用程序时调用的任何地方。在onResume()事件中也可能有用:

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

    mAudioManager.registerMediaButtonEventReceiver(mRemoteControlResponder);