Thursday, May 21, 2015

Starting an Activity from a BroadcastReceiver

The context we are getting from the onReceive method's parameter is not an Activity.

@Overridepublic void onReceive(Context context, Intent intent) {


If we want to start an Activity from here, the System will return an error:

context.startActivity(startActIntent);

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag exception - Android


Add a flag to the intent and things will work fine.

// must add flag FLAG_ACTIVITY_NEW_TASK if the context is not an Activity;startActIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


No comments:

Post a Comment