Android BroadcastReceiver没有收到意图

时间:2016-11-14 06:46:08

标签: android android-intent broadcastreceiver

以下是我在一个应用中的发件人:

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()中看到登录。

2 个答案:

答案 0 :(得分:0)

Intent intent = getIntent();
String name = intent.getStringExtra("name");

getIntent接收意图getExtra或GetStringExtra以使用“name”键获取.... 那么你需要

startActivity(intent)

答案 1 :(得分:0)

您是否曾使用mReceiver初始化new MyBroadcastReceiver()