简体   繁体   English

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

[英]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上运行


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. 现在我完全糊涂了。

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.

相关问题 android:ContentResolver在4.0上有效,但在2.3上无效 - android: ContentResolver works on 4.0 but not on 2.3 Android代码可在2.3上使用,但不能在4.0上使用 - Android Code Works On 2.3 But Not On 4.0 Android主要XML在2.3和4.0上的工作方式有所不同 - android main XML works diffrent on 2.3 and 4.0 菜单按钮2.3和4.0 - Menu button 2.3 and 4.0 Android 4.0中的类无响应-在2.3及以下版本中有效 - No response from class in Android 4.0 - works in 2.3 and below ActionBar - Android平板电脑2.3和4.0 - ActionBar - Android Tablet 2.3 and 4.0 Android应用程序只能在2.3上运行,而不能在4.0上运行 - Android app working on 2.3 but not on 4.0 Phonegap应用程序在运行Android 4.0时引发错误,但在Android 2.3及更低版本上可以正常运行 - Phonegap application throws error running Android 4.0, but works fine on Android 2.3 and below SHOUTCast Streaming Breaks in 4.0+,适用于2.3 - SHOUTCast Streaming Breaks in 4.0+, works in 2.3 Android我的应用程序可以在2.3上运行,但即使在4.0上也无法启动 - Android My application works on 2.3 fine but doesn't start even on 4.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM