How do I detect if the service is running on Android?

Asked 2 years ago, Updated 2 years ago, 49 views

How do I detect if the service is running on Android?

android

2022-09-22 22:03

1 Answers

I also thought a lot about this before, but I found a simple solution. Put a static variable in the service If the service is running, give the value to true, and if not, give the value to false.

 public static Boolean serviceRunning = false;

 public int onStartCommand(Intent intent, int flags, int startId) {

    serviceRunning = true;
    ...
}

 @Override
public void onDestroy()
{
    serviceRunning = false;

} 

Like this.


2022-09-22 22:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.