动态注册的广播接收器不起作用

时间:2013-01-06 16:40:49

标签: android android-intent runtime broadcastreceiver

我一直试图在这里找出问题几个小时。为什么这个广播接收者没有收到意图?

static class BReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {
        Toast.makeText(context, "Got the intent", Toast.LENGTH_LONG).show();
    }

}

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mLayout = new FrameLayout(this);
    setContentView(mLayout);
    final IntentFilter intentFilter = new IntentFilter("com.example.testui.action");
    getApplicationContext().registerReceiver(new BReceiver(), intentFilter);
    final Intent intent = new Intent(getApplicationContext(), BReceiver.class);
    intent.setAction("com.example.testui.action");
    getApplicationContext().sendBroadcast(intent);
}

PS。我没有在清单文件中定义这个接收器,因为严格来说,它不是强制性的。

1 个答案:

答案 0 :(得分:2)

尝试以这种方式发送广播:

getApplicationContext().sendBroadcast(new Intent("com.example.testui.action"));