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?
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:
I do not know whether ACTION_VIEW or FLAG_ACTIVITY_NEW_TASK are of any actual use here. The key to succeeding was
and of course FLAG_ACTIVITY_REORDER_TO_FRONT for not instantiating the activity again. Best of luck!
one cannot use the
Context
of theService
; was able to get the (package)Context
alike:I had the same problem, and want to let you know that none of the above worked for me. What worked for me was:
and in one my subclasses, stored in a separate file I had to:
All the other answers gave me a
nullpointerexception
.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
Alternately,
You can use your own Application class and call from wherever you needs (especially non-activities).
And register your Application class :
Then call :
From inside the Service class: