java - 使用非静态内部类在 MainActivity 中接收广播
问题描述
当用户与通知中的按钮交互时,我需要在活动中调用某些非静态方法,我尝试在一个独立的类中进行它的扩展BroadcastReceiver
,但我不知道如何在前面提到的活动上调用方法,我试图制作一个非静态的内部类,它首先编辑了清单文件:
<receiver android:name=".Activity$NotificationBroadcast" >
<intent-filter>
.
.
.
</intent-filter>
</receiver>
这给了我一个错误说:
FATAL EXCEPTION: main
Process: com.example.app, PID: 3189
java.lang.RuntimeException: Unable to instantiate receiver
com.example.app.Activity$NotificationBroadcast:
java.lang.InstantiationException:
课程是:
public class NotificationBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
if (intent.getAction().equals(Activity.ACTION1)){
// do things
} else if (intent.getAction().equals(Activity.ACTION1)){
// do things
}else if (intent.getAction().equals(Activity.ACTION2)){
// do things
}else if (intent.getAction().equals(Activity.ACTION3)){
// do things
}else if (intent.getAction().equals(Activity.ACTION4)){
// do things
}
}
}
解决方案
恐怕您不能将接收器作为内部类,因为当接收器被静态实例化时,“持有人”类也必须被实例化。活动实例仅在它应该处于活动状态时创建,这就是您遇到异常的原因。
如果您希望接收器与活动交互(通过在活动类中调用非静态方法),我建议您应该将接收器设置为非静态的。这意味着您需要在 中注册接收器的实例并在 中OnCreate()
注销它OnDestroy()
。
为了更好的设计,活动实例应该通过其构造函数作为接口传递给接收者,以便接收者无法访问整个活动对象,而只能访问功能。
清单应该有:
<receiver android:name=".NotificationBroadcast" android:enabled="false" />
交互的接口(例如 IWorker.java):
public interface IWorker {
public void doThis();
public void doThat();
}
接收器(它自己的一个类)接受 IWorker 并在收到广播时执行一些操作:
public class NotificationReceiver extends BroadcastReceiver {
public static final string ACTION1 = "com.yourpackage.action1";
public static final string ACTION2 = "com.yourpackage.action2";
private IWorker worker;
public NotificationReceiver() {
}
public NotificationReceiver(IWorker worker) {
this.worker = worker;
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION1)) {
worker.doThis();
} else if (intent.getAction().equals(ACTION2)) {
worker.doThat();
}
}
}
该活动在其生命周期中负责接收器:
public class MyActivity extends Activity implements IWorker {
private NotificationReceiver receiver;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// create the receiver
receiver = new NotificationReceiver(this);
IntentFilter filter = new IntentFilter();
filter.addAction(NotificationReceiver.ACTION1);
filter.addAction(NotificationReceiver.ACTION2);
// register it
registerReceiver(receiver, filter);
}
@override
protected void onDestroy() {
super.onDestroy();
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null;
}
}
@override
public void doThis() {
System.out.println("Doing this...");
}
@override
public void doThat() {
System.out.println("Doing that...");
}
}
PS 以上代码仅供参考,未经测试,可能无法编译。
推荐阅读
- reactjs - 无法获取查询字符串:路径名已填充但搜索为空
- vuejs2 - 基于选择值的Vue多选CSS
- c# - 无法添加扩展信息 Firefox geckodriver
- java - Spring Boot - 使用常量类读取自定义属性文件
- javascript - 为什么我的一个子组件能够获取 handleClick() 而其他子组件不能?
- c# - Selenium C#:按标题获取元素
- django - Kubernetes中的定期数据库备份?
- cython - Cython:使用我来自
, 不能让它工作 - python - 使用 pygame 显示具有“for”结构的字母的问题在哪里
- json - JOLT 基于 key 变换元素