Solve the error of the startActivity method of Context

The error is as follows

Java code  
  1. 02-10 13:26:11.048: ERROR/AndroidRuntime(17173): 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?  


The startActivity method of Context needs to start a new task.

If you use the startActivity method of Activity, there will be no restrictions, because Activity inherits from Context and has overloaded the startActivity method.

 

Solution:

Add a FLAG_ACTIVITY_NEW_TASK flag as the error prompts

Java code   Favorite code
  1. public void onReceive(Context context, Intent intent) {  
  2.     ……  
  3.     Intent startiPhone = new Intent(context, Iphone.class);  
  4.     startiPhone.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  5.     context.startActivity(startiPhone);  
  6.     ……  
  7. }  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324531462&siteId=291194637