在课程中,我想向我的活动发送意图:
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类根本不会打印任何内容。
有谁知道为什么会这样? 提前谢谢!
答案 0 :(得分:1)
你有这个Manifest吗?
<receiver android:name="xxx.MyReceiver">
<intent-filter>
<action android:name="coinchutc.ANNONCE" />
</intent-filter>
</receiver>
答案 1 :(得分:0)