165

Android:

public class LocationService extends Service {

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        startActivity(new Intent(this, activity.class));
    }
}

I launched this service from Activity

In Activity if condition satisfies start

startService(new Intent(WozzonActivity.this, LocationService.class));

from my LocationService mentioned above could not launch Activity, how can I get context of current running Activity in service class?

Ryan M
  • 15,686
  • 29
  • 53
  • 64
d-man
  • 55,965
  • 82
  • 204
  • 290

8 Answers8

368

From inside the Service class:

Intent dialogIntent = new Intent(this, MyActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);

But, this does not work from Android 10+ due to battery optimisation restrictions

Sambhav. K
  • 3,046
  • 2
  • 5
  • 29
d-man
  • 55,965
  • 82
  • 204
  • 290
20

I had the same problem, and want to let you know that none of the above worked for me. What worked for me was:

 Intent dialogIntent = new Intent(this, myActivity.class);
 dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 this.startActivity(dialogIntent);

and in one my subclasses, stored in a separate file I had to:

public static Service myService;

myService = this;

new SubService(myService);

Intent dialogIntent = new Intent(myService, myActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myService.startActivity(dialogIntent);

All the other answers gave me a nullpointerexception.

Nikhil Agrawal
  • 25,020
  • 19
  • 86
  • 120
Andy
  • 2,399
  • 1
  • 23
  • 25
20

UPDATE ANDROID 10 AND HIGHER

Start an activity from service (foreground or background) is no longer allowed.

There are still some restrictions that can be seen in the documentation

https://developer.android.com/guide/components/activities/background-starts

José Braz
  • 405
  • 3
  • 7
  • 1
    @RahulBhobe I found this answer helpful, because adding the `SYSTEM_ALERT_WINDOW` (and enabling it in the settings) solved my issue on Android 10. – Daniel F Sep 05 '20 at 07:32
  • 1
    SYSTEM_ALERT_WINDOW can not be granted in Go Edition devices. – Shrdi Mar 29 '21 at 11:25
  • @Shrdi, so, do you have any solutions for AndroidGo? – AlexS Aug 12 '21 at 16:45
  • @AlexS I don't think this can work on Android 10+Go because this is the system limitation. Any feature about this should offer another alternative way or hint, user might not agree this permission as well. – Shrdi Aug 14 '21 at 04:30
10

Another thing worth mentioning: while the answer above works just fine when our task is in the background, the only way I could make it work if our task (made of service + some activities) was in the foreground (i.e. one of our activities visible to user) was like this:

    Intent intent = new Intent(storedActivity, MyActivity.class);
    intent.setAction(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    storedActivity.startActivity(intent);

I do not know whether ACTION_VIEW or FLAG_ACTIVITY_NEW_TASK are of any actual use here. The key to succeeding was

storedActivity.startActivity(intent);

and of course FLAG_ACTIVITY_REORDER_TO_FRONT for not instantiating the activity again. Best of luck!

kellogs
  • 2,767
  • 2
  • 34
  • 50
  • 1
    This does not work. All this gives me is the error "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?" – Cerin Jul 18 '12 at 00:49
  • 3
    This is because the second call to setFlags overrides the first one. This solution should use addFlags, or a single call to set flags that passes Intent.FLAG_ACTIVITY_NEW_TASK && Intent.FLAG_ACTIVITY_REORDER_TO_FRONT. – Fergusmac Jul 12 '13 at 00:23
  • 8
    You don't want to combine flags with &&; you want to use | (a single bar for bitwise or) – Jon Watte Sep 20 '13 at 21:02
  • Intent intent = new Intent(storedActivity, MyActivity.class); intent.setAction(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); storedActivity.startActivity(intent); – Prakhar Kulshreshtha Oct 03 '17 at 10:49
1

one cannot use the Context of the Service; was able to get the (package) Context alike:

Intent intent = new Intent(getApplicationContext(), SomeActivity.class);
Martin Zeitler
  • 59,798
  • 15
  • 122
  • 186
0

Alternately,

You can use your own Application class and call from wherever you needs (especially non-activities).

public class App extends Application {

    protected static Context context = null;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
    }

    public static Context getContext() {
        return context;
    }

}

And register your Application class :

<application android:name="yourpackage.App" ...

Then call :

App.getContext();
james
  • 1,857
  • 2
  • 19
  • 26
0

You can also use getApplicationContext() method in your Service to run the startActivity() method as written below:

Intent myIntent = new Intent();
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(myIntent);
Suraj Gaur
  • 21
  • 4
-1

If you need to recal an Activity that is in bacgrounde, from your service, I suggest the following link. Intent.FLAG_ACTIVITY_NEW_TASK is not the solution.

https://stackoverflow.com/a/8759867/1127429

Community
  • 1
  • 1
GMG
  • 1,258
  • 12
  • 18