似乎很容易。为什么我的接收者会收到意图?我在主要活动类的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);
答案 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
。