以下是我在一个应用中的发件人:
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()中看到登录。
答案 0 :(得分:0)
Intent intent = getIntent();
String name = intent.getStringExtra("name");
getIntent接收意图getExtra或GetStringExtra以使用“name”键获取.... 那么你需要
startActivity(intent)
答案 1 :(得分:0)
您是否曾使用mReceiver
初始化new MyBroadcastReceiver()
?