如何将参数传递给广播接收器的子类? [英] How to pass a parameter to a subclass of BroadcastReceiver?

查看:170
本文介绍了如何将参数传递给广播接收器的子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法时pressed我耳机按键得到由我的应用程序认可,但其中一个按钮需要调用这是在MyCustomActivity的方法。问题是的onReceive的第一个参数是不能转换到活动并使用MyCustomActivity的的的内部类荣获上下文Android中4.1 的将不起作用,除非它是静态的(其具有无法访问MyCustomActivity的方法的同样的问题。

I managed to get my headset buttons get recognized by my app when pressed, but one of the buttons needs to call a method that's in MyCustomActivity. The problem is onReceive's 1st parameter is a Context that cannot be cast to Activity and using a MyCustomActivity's inner class won't work in Android 4.1 unless it is static (which has the same problem of inability to access MyCustomActivity's method.

所以,剩下的唯一选择,我(以支持2.x和4.1)是传递活动作为参数的 RemoteControlReceiver

So the only option left for me (in order to support both 2.x and 4.1) is to pass the activity as a parameter to RemoteControlReceiver.

但我怎么做,当初始化它的唯一途径是通过:

But how do I do that, when the only way to instantiate it is via:

private ComponentName mRemoteControlReceiver = new ComponentName(this, RemoteControlReceiver.class);

不接受任何额外的参数?

Which doesn't accept any additional parameters?

不知道如何解决此限制?

Any idea how to work around this limitation?

注意:如果我尝试定义 RemoteControlReceiver 为具有一个参数的构造函数,我收到以下异常:

Note: If I try to define RemoteControlReceiver as having a constructor with a parameter, I receive the following exception:

E/AndroidRuntime(2836): java.lang.RuntimeException: Unable to instantiate receiver com.example.RemoteControlReceiver: java.lang.InstantiationException: can't instantiate class com.example.RemoteControlReceiver; no empty constructor

Caused by:
E/AndroidRuntime(2836): Caused by: java.lang.InstantiationException: can't instantiate class com.example.RemoteControlReceiver; no empty constructor
E/AndroidRuntime(2836):     at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(2836):     at java.lang.Class.newInstance(Class.java:1319)
E/AndroidRuntime(2836):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2205)

所以,很显然,这个新的registerMediaButtonEventReceiver要求(在Android的4.1引入的)期望一个空构造

请问有没有办法解决此问题?

Is there no way to work around this?

例如,是否有一种方式来获得实际RemoteControlReceiver对象的引用(通过 mAudioManager.registerMediaButtonEventReceiver间接实例化())?所以,我可以用一个访问设置后RemoteControlReceiver的的数据成员的它已被实例化?

For example, is there a way to get a reference to the actual RemoteControlReceiver object (instantiated indirectly via mAudioManager.registerMediaButtonEventReceiver())? So that I can use an accessor to set a data-member of RemoteControlReceiver after it has been instantiated?

推荐答案

<一个href=\"http://developer.android.com/reference/android/media/AudioManager.html#registerMediaButtonEventReceiver%28android.content.ComponentName%29\">registerMediaButtonEventReceiver需要在应用程序清单中声明的​​广播接收器。这意味着接收机必须是一个独立的阶级,这意味着它什么都不知道关于你的当前活动或服务。

registerMediaButtonEventReceiver requires the BroadcastReceiver to be declared in the application manifest. This means that the receiver must be a standalone class, meaning it knows nothing about your current activity or service.

为了得到这个消息,您的活动或服务,您有多种选择:

In order to get this message to your activity or service, you have a number of options:


  • 使用静态全局的活动或服务,这样的接收器可以将邮件转发给它。这通常不是一个好主意,因为它会导致泄漏,当你想以后改变code不是很适应。静是一般应​​避免。

  • Use a static global for the activity or service so the receiver can forward the message to it. This is generally not a good idea as it leads to leaks and isn't very adaptable when you want to change the code later. Statics are generally to be avoided.

重新广播消息到具体的类别,这恰好是一个内部类,你要调用的活动或服务的。例如。在广播接收器的registerMediaButtonEventReceiver:

Re-broadcast the message to a specific class, which happens to be an inner class of the activity or service you want to invoke. E.g. in the BroadcastReceiver for registerMediaButtonEventReceiver:

// Standalone class, declared in the manifest
public class ButtonReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, final Intent intent) {
        Intent intent = new Intent();
        intent.setAction("com.foo.ACTION");

        // Rebroadcasts to your own receiver. 
        // This receiver is not exported; it'll only be received if the receiver is currently registered.
        context.sendBroadcast(intent); 
    }
}


而在你的活动:

    class MyActivity extends Activity {
        private BroadcastReceiver myReceiver = new BroadcastReceiver() {
            @Override
             public void onReceive(final Context context, final Intent intent) {
                MyActivity.this.onMessageReceived();
             }
        }
        @Override
        protected void onResume() {
            registerReceiver(myReceiver, new IntentFilter("com.foo.ACTION"));
        }

        @Override
        protected void onPause() {
            unregisterReceiver(myReceiver);
        }

        private void onMessageReceived() {
        }
    }


  • 上述方法类似,它不一定必须是一个广播,也可以是传递到活性的意图,这取决于你的使用情况。要做到的,而不是使用sendBroadcast这个,你(如果你使用的服务或startService)使用startActivity。

  • 这篇关于如何将参数传递给广播接收器的子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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