Android 2.3.3
Eclipse Version: 3.7.0
LogCat
BootBroadcastReceiver继承自android.content.BroadcastReceiver,处理广播事件,部分代码如下:
public void onReceive(Context context, Intent intent) {
……
Intent startTaobao = new Intent(context, Start.class);
context.startActivity(startTaobao);
……
}
LogCat 报错信息:
02-10 17:13:20.417: DEBUG/AndroidRuntime(5066): Shutting down VM
02-10 17:13:20.417: WARN/dalvikvm(5066): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): FATAL EXCEPTION: main
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): java.lang.RuntimeException: Unable to start receiver com.taobao.nokia.BootBroadcastReceiver: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1805)
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): at android.app.ActivityThread.access$2400(ActivityThread.java:117)
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): at android.os.Looper.loop(Looper.java:123)
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): at java.lang.reflect.Method.invoke(Method.java:507)
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): at dalvik.system.NativeStart.main(Native Method)
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): at android.app.ContextImpl.startActivity(ContextImpl.java:621)
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): at com.taobao.nokia.BootBroadcastReceiver.onReceive(BootBroadcastReceiver.java:64)
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): ... 10 more
02-10 17:13:20.478: WARN/ActivityManager(68): Force finishing activity com.taobao.nokia/.Start
主要错误:
02-10 17:13:20.448: ERROR/AndroidRuntime(5066): Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
发生错误原因分析:
Content的startActivity方法,需要开启一个新的task。如果使用 Activity的startActivity方法,不会有任何限制,因为Activity继承自Context,重载了startActivity方法。
解决办法:
按照错误提示,添加一个FLAG_ACTIVITY_NEW_TASK flag
public void onReceive(Context context, Intent intent) {
……
Intent startTaobao = new Intent(context, Start.class);
startTaobao.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startTaobao);
……
}