Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW _TASK flag.

在Activity中使用startActivity()方法不会有任何限制,因为Activity重载了Context的startActivity()方法。但是如果是在其他地方(如Widget或Service、BroadcastReceiver中)使用startActivity()方法,就会报错:

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

这时就需要为Intent设置一个FLAG_ACTIVITY_NEW_TASK的flag:

Intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

这样就不会报错了,可以顺利的startActivity()。

posted @ 2015-07-11 08:49  第壹时间  阅读(2127)  评论(0编辑  收藏  举报