按下后,我设法通过我的应用获取headset buttons get recognized,但其中一个按钮需要调用MyCustomActivity中的方法。问题是onReceive的第一个参数是一个Context,它不能转换为Activity并使用MyCustomActivity的内部类 won't work in Android 4.1,除非它是静态的(它具有无法访问MyCustomActivity方法的相同问题)。
因此,我唯一的选择(为了支持2.x和4.1)是将活动作为参数传递给RemoteControlReceiver。
但是我怎么做,当实例化它的唯一方法是通过:
private ComponentName mRemoteControlReceiver = new ComponentName(this, RemoteControlReceiver.class);
哪个不接受任何其他参数?
知道如何解决这个限制吗?
注意:如果我尝试将RemoteControlReceiver
定义为包含带参数的构造函数,则会收到以下异常:
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中引入)需要空构造函数。
有没有办法解决这个问题?
例如,有没有办法获取对实际RemoteControlReceiver对象的引用(通过mAudioManager.registerMediaButtonEventReceiver()
间接实例化)?这样我可以使用访问器在实例化之后设置RemoteControlReceiver 的数据成员吗?
答案 0 :(得分:7)
registerMediaButtonEventReceiver要求在应用程序清单中声明BroadcastReceiver。这意味着接收器必须是独立的类,这意味着它对您当前的活动或服务一无所知。
为了将此消息发送到您的活动或服务,您有许多选择:
为活动或服务使用静态全局,以便接收方可以将消息转发给它。这通常不是一个好主意,因为它会导致泄漏,并且当您想稍后更改代码时,它不具有很强的适应性。通常要避免使用静力学。
将消息重新广播到特定的类,该类恰好是您要调用的活动或服务的内部类。例如。在BroadcastReceiver中为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() {
}
}