接收者没有收到意图

时间:2017-08-10 22:49:03

标签: android

似乎很容易。为什么我的接收者会收到意图?我在主要活动类的onCreate方法中声明了这个接收器。

    IntentFilter filter = new IntentFilter();
    filter.addAction(WidgetProvider.ACTION_OPEN_ACTIVITY);

    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            // No toast is shown. 
            Toast.makeText(MainActivity.this,"Intent received",Toast.LENGTH_SHORT).show();
        }
    }, filter);

    Intent intent = new Intent(this,MainActivity.class);
    intent.setAction(WidgetProvider.ACTION_OPEN_ACTIVITY);
    sendBroadcast(intent);

1 个答案:

答案 0 :(得分:0)

替换:

Intent intent = new Intent(this,MainActivity.class);
intent.setAction(WidgetProvider.ACTION_OPEN_ACTIVITY);

使用:

Intent intent = new Intent(WidgetProvider.ACTION_OPEN_ACTIVITY);

您无法使用动态注册的接收方明确Intent,而MainActivity不是BroadcastReceiver