Start Activity in BroadcastReceiver:
If you start an Activity in the onReceive() method of BroadcastReceiver as follows
Intent intent=new Intent(context,AnotherActivity.class);
context.startActivity(intent);
Exception information can be captured:
android.util.AndroidRuntimeException: Calling startActivity () from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK
flag. Is this
really what you want? Start Activity in the onReceive() method of BroadcastReceiver should be written as: Intent intent=new Intent(context,AnotherActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent);
The phenomenon and solution of the problem were described before, and now try to explain the reason:
1 In normal circumstances, the Context of the previous Activity must be available to start the next Activity
2 But in BroadcastReceiver, there is no Context of the Activity
3 For The startActivity() method is described in the source code:
Note that if this method is being called from outside of an
{@link android.app.Activity} Context, then the Intent must include
the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag . This is because,
without being started from an existing Activity, there is no existing
task in which to place the new activity and thus it needs to be placed
in its own separate task. To
put it bluntly, if this flag is not added, there will be no Task To store the newly started Activity.
4 In fact, the flag is the same as setting the Activity's LaunchMode to SingleTask.
From: http://blog.csdn.net/cnmilan/article/details/50617802
Start Activity in BroadcastReceiver
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326152897&siteId=291194637
Recommended
Ranking