How do I know if my service is running?

In my code, I start my service conditionally as follows:

Intent intent = new Intent(context, MyService.class); context.startService(intent); 

Can you please tell me if you can find out if I started ONLY SERVICE SERVICES before I start my service TWICE?

Thanks.

+4
source share
2 answers

the service does not start twice if it is already running, even if you call startService () several times, you only need one stopService () to stop it.

see here and here .

+8
source

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 you can create as many times as you like (perhaps because I create various running objects inside it).

+5
source

Source: https://habr.com/ru/post/1341373/


All Articles