这是我在一个应用程序发送者:Android的广播接收器没有收到意向
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setAction("my.pack.SET_DONE");
intent.putExtra("name", "PWM");
intent.addCategory(Intent.CATEGORY_DEFAULT);
mContext.sendBroadcast(intent);
这里是我在其他应用程序接收器:
private Object mySyncObj = new Object();
private AtomicBoolean mySetDone = new AtomicBoolean(false);
static private MyBroadcastReceiver mReceiver;
public class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(LOG_TAG, "action = " + action);
if (action.equalsIgnoreCase("my.pack.SET_DONE")){
synchronized (mySyncObj) {
mySetDone.set(true);
mySyncObj.notify();
}
}
}
}
接收器的动态注册
mIntentFilter = new IntentFilter("my.pack.SET_DONE");
mIntentFilter.addCategory(Intent.CATEGORY_DEFAULT);
ctx.registerReceiver(mReceiver, mIntentFilter);
mySetDone.set(false);
// do something and then wait for the broadcast
synchronized (mySyncObj) {
try {
while(!mySetDone.get()) {
mySyncObj.wait();
}
} catch (InterruptedException ie) {}
}
从logcat,我可以看到广播的意图是好的,但我从来没有看到onReceive()日志。
你在哪里注册你的接收器?如果它在一个活动中,则它必须活跃才能接收该意图。如果不是,你需要在'Manifest'中注册你的接收器。描述你想达到的目标。 –
如果一切正常,您应该重新启动手机并重试! – javadaskari