在课堂上,我想向我的活动发送一个意图:
Intent broadcast = new Intent();
broadcast.setAction("coinchutc.ANNONCE");
Log.d("JoueurAgent", "Sending broadcast " + broadcast.getAction());
context.sendBroadcast(broadcast);
日志可以正确打印出消息。
在我的 Activity 类中,我声明了一个自定义广播接收器:
myReceiver = new MyReceiver();
MyReceiver 类是这样的:
private class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("PartieActivity", "receive");
String action = intent.getAction();
if (action.equalsIgnoreCase("coinchutc.ANNONCE")) {
Log.d("PartieActivity", "Receive " + action);
annoncer();
}
}
}
我已经在类的 onCreate() 方法中注册了这样的活动:
IntentFilter annonceFilter = new IntentFilter();
annonceFilter.addAction("coinchutc.ANNONCE");
registerReceiver(myReceiver, annonceFilter);
但是 MyReceiver 类中的 Log 根本不打印任何内容。
有谁知道为什么会这样?提前致谢!