在Android&GT捕获媒体按钮; = 4.0(适用于2.3) [英] Capture media button on Android >=4.0 (works on 2.3)

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

问题描述

我写了一些服务,它使用了BroadcastReceiver捕捉到的媒体按钮(从耳机播放按钮),它完美的作品在Android 2.3.x(HTC的Nexus One和HTC Desire的)

当我试图在Android 4.0.3(三星Nexus S)运行它不工作(我的应用程序不会收到意图android.intent.action.MEDIA_BUTTON和播放按钮的行为像往常一样:停止/启动音乐)。

清单的内容:

  ...
<应用
    机器人:图标=@可绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME>
    <接收机器人:名称=。buttonreceiver.MediaButtonIntentReceiver>
        <意图过滤器的Andr​​oid版本:优先=10000>
            <作用机器人:名称=android.intent.action.MEDIA_BUTTON/>
        &所述; /意图滤光器>
    < /接收器>
...
 

有没有办法让它在Android 4.0.3工作


编辑:我尝试提出解决办法,我已经添加了行动,并运行它,但我的接收器仍然没有收到意图。更重要的是奇怪的登记接收由code也不起作用:

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.about_and_activati​​on_view);

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

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

现在我完全糊涂了。

解决方案

请确保你在你的应用程序的活动,并且用户试图preSS这个按钮的时候运行此活动。在此之前,你的<接收器> 将不会收到任何广播


更新

在Android 4.0及更高版本,看来你还需要调用<一个href="http://developer.android.com/reference/android/media/AudioManager.html#registerMediaButtonEventReceiver%28android.content.ComponentName%29"><$c$c>registerMediaButtonEventReceiver() AudioManager 以接收的事件。该国将举行,直到别的电话 registerMediaButtonEventReceiver()或直到调用 unregisterMediaButtonEventReceiver()

例如,像这样的活动:

 公共类MediaButtonActivity延伸活动{
  @覆盖
  公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    ((AudioManager)getSystemService(AUDIO_SERVICE))。registerMediaButtonEventReceiver(新的组件名(
                                                                                                       本,
                                                                                                       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&GT捕获媒体按钮; = 4.0(适用于2.3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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