解决在非Activity中使用startActivity

错误提示信息:

        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?

原因:

     activity继承了context重载了startActivity方法,如果使用acitvity中的startActivity,不会有任何限制。

     而如果直接使用context的startActivity则会报上面的错误,根据错误提示信息,可以得知,如果要使用这种方式需要打开新的TASK。

故,解决方法:

   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

  context.startActivity(intent);