安卓的BroadcastReceiver不会注册 [英] Android BroadcastReceiver won't register

查看:144
本文介绍了安卓的BroadcastReceiver不会注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个简单的广播接收器添加到我的音频应用程序,这样,当用户点击他们的ACTION_MEDIA_BUTTON自己的耳机,我可以静音一切。我读过,你可以在code寄存器它在清单中,或动态。我已经下降登记在code的路径,因为我需要调用在我的主要活动类的方法来应对媒体按钮preSS。出于某种原因,但是,我的BroadcastReceiver是不会注册,我找不到任何可以解释为什么(白发增加)。

I'm trying to add a simple broadcast receiver to my audio application, so that I can mute everything when the user clicks their ACTION_MEDIA_BUTTON on their headset. I've read that you can either register it in the manifest, or dynamically in the code. I have gone down the path of registering it in the code, as I need to call methods within my main activity class to react to the media button press. For some reason however, my BroadcastReceiver just will not register, and I can't find anything that explains why (grey hairs increasing).

下面是我在MainActivity.java:

The following is what I have in MainActivity.java:

public class MainActivity extends Activity {

  public IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);

  public BroadcastReceiver MediaButtonIntentReceiver =
            new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {

                    String intentAction = intent.getAction();
                    if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
                        KeyEvent event = (KeyEvent) intent
                                .getParcelableExtra(Intent.EXTRA_KEY_EVENT);
                        int action = event.getAction();
                        if (action == KeyEvent.ACTION_DOWN) {

                            Log.e("INFO", "Media Button Pressed");
                            MuteAll();

                        }


                }
           }
  };

  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);       

      // Register media button event receiver
      intentFilter.addAction("android.intent.action.ACTION_MEDIA_BUTTON");
      intentFilter.setPriority(10000);
      this.registerReceiver(MediaButtonIntentReceiver, intentFilter);

      }

@Override
protected void onDestroy() {
    super.onDestroy();

    // Unregister media button event receiver
    unregisterReceiver(MediaButtonIntentReceiver);

    }
};

我确信的BroadcastReceiver没有注册,因为包装的寄存器如下给我敬酒,确认它为空:

I am certain that the BroadcastReceiver doesn't register, as wrapping the register as below gives me a toast confirming it is null:

  if (registerReceiver(MediaButtonIntentReceiver, intentFilter) == null) 
  {
      Toast.makeText(this, "Could not register receiver", Toast.LENGTH_LONG).show();
  } else {
      Toast.makeText(this, "Receiver registered", Toast.LENGTH_LONG).show();
  }

编辑: 我也尝试了基于​​以下的建议到目前为止:

I've also tried the following based on suggestions so far:

通读 - http://developer.android.com/training /managing-audio/volume-playback.html 我试着像这样的清单内注册我的接收器...

Reading through - http://developer.android.com/training/managing-audio/volume-playback.html I tried registering my receiver within the manifest like so...

<receiver android:name="com.mydomain.myapp.MainActivity$MediaButtonIntentReceiver">
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_BUTTON" />
    </intent-filter>
</receiver>

And then added the following example code:
public AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);
...
// Start listening for button presses
am.registerMediaButtonEventReceiver(RemoteControlReceiver);

Eclipse的抱怨mContext没有解决任何东西,所以我增加了以下内容:

Eclipse complained that mContext didn't resolve to anything, so I added the following:

private Context mContext;

然后抱怨mContext.getSystemService(Context.AUDIO_SERVICE)部分,称类型不匹配:不能从对象转换为AudioManager

Then it complained about the "mContext.getSystemService(Context.AUDIO_SERVICE)" portion, saying "Type mismatch: cannot convert from Object to AudioManager"

所以我增加了一个强制转换为AudioManager:

So I added a cast to AudioManager:

public AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);

,然后添加建议的接收器注册code:

And then added the suggested receiver registration code:

am.registerMediaButtonEventReceiver(MediaButtonIntentReceiver);

要它抱怨registerMediaButtonEventReceiver的说法,在类型AudioManager的方法registerMediaButtonEventReceiver(组件名)不适用于参数(BroadcastReceiver的)

To which it complained about "registerMediaButtonEventReceiver" saying "The method registerMediaButtonEventReceiver(ComponentName) in the type AudioManager is not applicable for the arguments (BroadcastReceiver)"

显然,我在这里做得不对。我已经进入了他们的榜样code,如图,但它甚至不进行编译。

Clearly I'm doing something wrong here. I've entered their example code as shown, yet it doesn't even compile.

---编辑完-----------------------------

--- END EDIT -----------------------------

希望有人在那里可以请帮助我。请让我知道,如果我需要进一步提供什么。

Hoping someone out there can please help me. Please let me know if I need to supply anything further.

推荐答案

一个接收器的 ACTION_MEDIA_BUTTON 的行动应与 AudioManager registerMediaButtonEventReceiver()方法,而不是在<$ C常规 registerReceiver()方法$ C>活动。不同于一般的动态接收登记,但是,这种方法需要代替类的实例的类作为参数,。最简单的方法是创建一个单独的类文件是:

A Receiver for the ACTION_MEDIA_BUTTON action should be registered with AudioManager's registerMediaButtonEventReceiver() method, instead of the regular registerReceiver() method in the Activity. Unlike normal dynamic Receiver registration, however, this method takes the class as the parameter, instead of an instance of the class. The easiest way to this is to create a separate class file for it:

public class MediaButtonIntentReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        ...
    }
}

和我们需要这种接收器在清单中列出,以及:

And we would need this Receiver listed in the manifest, as well:

<receiver android:name=".MediaButtonIntentReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_BUTTON" />
    </intent-filter>
</receiver>

现在,在我们已经提到的链接的例子是错误的,因为 registerMediaButtonEventReceiver()方法需要一个组件名对象,接收器类本身不只是名字。我们需要改变的例子如下:

Now, the example in the link we've referred to is wrong, as the registerMediaButtonEventReceiver() method is expecting a ComponentName object, not just the name of the Receiver class itself. We need to change the example as follows:

AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
am.registerMediaButtonEventReceiver(
    new ComponentName(this, MediaButtonIntentReceiver.class));

和,因为我们已经建立了,你不需要 mContext 字段,因为你是在一个活动语境,并且可以只用 getSystemService()无资质。您也可以废除了的IntentFilter 对象,如清单上市需要的是照顾了。

And, as we've established, you don't need the mContext field, as you are in anActivity Context, and can just use getSystemService() without qualification. You can also do away with the IntentFilter object, as the listing in the manifest takes care of that already.

这篇关于安卓的BroadcastReceiver不会注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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