我试图自动启动使用)在的onPause实施(以下BroadcastReceiver的方法充电我nightclock应用:startActivity()从广播接收器
BroadcastReceiver test = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
unregisterReceiver(this);
Intent i = new Intent(context, NightClock.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
};
registerReceiver(test, new IntentFilter(Intent.ACTION_POWER_CONNECTED));
的的onReceive()方法时,会引发USB电缆是插入,但该活动未启动。然而,日志显示了这一点:
I/ActivityManager( 79): Starting activity: Intent { flg=0x10000000 cmp=com.meins.nightclock/.NightClock }
任何想法为什么日志说活动已开始,但没有任何反应?
请更新您的问题,更多来自LogCat - 此行之前的几行以及此行之后的十几行。另外,为什么'FLAG_ACTIVITY_NEW_TASK'?还有,当ACTION_POWER_CONNECTED发生时,注册此'BroadcastReceiver'的活动还在吗? – CommonsWare 2010-10-03 13:50:23
连接电源时,LogCat中没有更多行。 'BroadcastReceiver'被注册在它应该开始的同一个活动中。此活动仍在后台运行(LogCat应用程序已置于前台)。如果这个活动在任务管理器中被终止,那么'BroadcastReceiver'似乎根本不会被触发。这种方法是否从一开始就错误地自动启动我的应用程序? – Gubbel 2010-10-03 14:47:20