此代码不起作用。会发生什么?
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(br, new IntentFilter("a.b.c.d"));
Intent it = new Intent("a.b.c.d");
startActivity(it);
}
BroadcastReceiver br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "test", Toast.LENGTH_SHORT);
}
};
}
ERROR:
01-22 10:55:21.812: ERROR/AndroidRuntime(10405): java.lang.RuntimeException: Unable to start activity ComponentInfo{sample.intent.broadcast/sample.intent.broadcast.Main}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=a.b.c.d }
答案 0 :(得分:0)
如果您只是尝试测试接收器,请将startActivity(it)
替换为sendBroadcast(it)
。
答案 1 :(得分:0)
a.b.c.d不是注册意图。使用存在的活动。检查清单文件或标准应用程序的意图列表。