Reputation: 110520
In my code, I start my service conditionally like this:
Intent intent = new Intent(context, MyService.class);
context.startService(intent);
Can u please tell me if it is possible to find out if I have started the SAME Service before so that I don't start my service TWICE?
Thank you.
Upvotes: 5
Views: 3369
Reputation: 491
Check this:
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.example.MyService".equals(service.service.getClassName())) {
return true;
}
}
return false;}
@bigstones: I don't know how, but I have a service that it can be created how many times as you like (maybe because I create inside it various runnable objects).
Upvotes: 5