Calling startActivity() from outside of an Activity
在Activity中使用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);

这样就OK了

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity
上图为打开wifi设置界面

相关文章:

  • 2021-08-15
  • 2021-08-15
  • 2021-08-15
  • 2021-08-15
  • 2021-08-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-17
  • 2021-07-06
  • 2021-08-15
  • 2021-07-15
  • 2021-08-15
相关资源
相似解决方案