Content 의 startActivity 방법 은 FLAG 를 추가 해 야 합 니 다.ACTIVITY_NEW_TASK flag

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);
		……
	}

좋은 웹페이지 즐겨찾기