[英]Capture media button on Android >=4.0 (works on 2.3)
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捕获媒体按钮之一(来自耳机的“播放按钮”),它在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.action.MEDIA_BUTTON”和“播放”按钮表现如常:停止/启动音乐)。
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上运行
@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. 现在我完全糊涂了。
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 . 在此之前,您的
<receiver>
将不会收到任何广播 。
UPDATE UPDATE
On Android 4.0 and higher, it appears that you also need to call registerMediaButtonEventReceiver()
on AudioManager
in order to receive the events. 在Android 4.0及更高版本中,您似乎还需要在
AudioManager
上调用registerMediaButtonEventReceiver()
才能接收事件。 That state will hold until something else calls registerMediaButtonEventReceiver()
or until you call unregisterMediaButtonEventReceiver()
. 该状态将持续,直到其他调用
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事件。
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. 将此添加到主活动中的onCreate()或运行应用程序时调用的任何位置。 Could be useful in the onResume() event too:
也可以在onResume()事件中有用:
mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
mRemoteControlResponder = new ComponentName(getActivity().getPackageName(), BroadcastReceiver.class.getCanonicalName());
mAudioManager.registerMediaButtonEventReceiver(mRemoteControlResponder);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.