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()。

原文地址:https://www.cnblogs.com/diyishijian/p/4638011.html