DixieFlatline
DixieFlatline

Reputation: 8035

How to launch activity from service properly?

I have a activity that can launch a service after the user presses the button. In this service, i have a Timer that runs every 10s. I want to run another activity (google maps) from this service every 10s. If i put it in service i get this error:

12-29 10:09:21.369: ERROR/AndroidRuntime(235): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

Where shoud i put my code to launch activity?

Upvotes: 0

Views: 6641

Answers (1)

st0le
st0le

Reputation: 33545

As the error message said, You need set the flag in your Intent

    Intent intent = new Intent(MyService.this,MyActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

Upvotes: 5

Related Questions