Android 上的“捕获媒体"按钮 >=4.0(适用于 2.3) [英] Capture media button on Android >=4.0 (works on 2.3)

查看:25
本文介绍了Android 上的“捕获媒体"按钮 >=4.0(适用于 2.3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一些使用 BroadcastReceiver 来捕获媒体按钮之一(耳机中的播放按钮")的服务,它在 android 2.3.x(HTC Nexus One 或 HTC Desire)上完美运行

当我尝试在 Android 4.0.3 (Samsung Nexus S) 上运行时,它不起作用(我的应用程序没有收到意图android.intent.action.MEDIA_BUTTON"和播放"按钮的行为像往常一样:停止/开始音乐).

清单内容:

<块引用>

<代码>...<申请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"/></意图过滤器></接收器>...

有没有办法让它在 android 4.0.3 上运行<小时>我已经尝试了建议的解决方案,我已经添加了操作并运行它,但我的接收器仍然没有收到意图.更奇怪的是,通过代码注册接收器也不起作用:

@Overrideprotected 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));}

现在我完全糊涂了.

解决方案

确保您的应用中有一个 Activity,并且用户在尝试按下该按钮之前运行了该 Activity.在此之前,您的 将不会收到任何广播.

<小时>

更新

在 Android 4.0 及更高版本上,您似乎还需要调用 registerMediaButtonEventReceiver() on AudioManager 以接收事件.该状态将一直保持,直到其他东西调用 registerMediaButtonEventReceiver() 或直到您调用 unregisterMediaButtonEventReceiver().

例如,这样的活动:

public class MediaButtonActivity extends Activity {@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);((AudioManager)getSystemService(AUDIO_SERVICE)).registerMediaButtonEventReceiver(new ComponentName(这个,MediaButtonReceiver.class));}}

将启用清单注册的 MediaButtonReceiver 以获取 ACTION_MEDIA_BUTTON 事件.

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.

解决方案

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.

这篇关于Android 上的“捕获媒体"按钮 >=4.0(适用于 2.3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆