Android tip #025 – Launch Activity from a service

Platform/Language: Java/Android

Description: if we have a service in background and we need to launch an Activity in foreground, we need to add the tag FLAG_ACTIVITY_NEW_TASK to the Intent.

Code:

MyService.java

1
2
3
4
5
6
7
8
9
10
11
public class MyService extends Service {
  /* ... */

  private void launchActivity() {
    Intent intent = new Intent(this, MyActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    this.startActivity(intent);
  }

  /* ... */
}

Leave A Reply