這是我在一個應用程序發送者: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