Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android how to check if service is running

I have an app that gets GPS co-ordinates. It works fine but i want to test from within the calling activity if the service is running. I have the following code that i thought would check but it is returning false when the GPS icon in the notification bar is still flashing. Has anyone any ideas why? Thanks in advance

private boolean isGpsServiceRunning() {
          ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
          for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if ("LocationService".equals(service.service.getClassName())) {
              return true;
            }
          }
          return false;
        }

.

LocalBroadcastManager.getInstance(getApplicationContext())
                            .registerReceiver(
                                    locationChangereceiver,
                                    new IntentFilter(
                                            LocationChangeIntent.ACTION_LOCATION_CHANGE));
                    startService(new Intent(this, LocationService.class));
like image 736
turtleboy Avatar asked Dec 31 '25 01:12

turtleboy


1 Answers

You should compare against LocationService.class.getName(), instead of using a hard coded value. Also, the value that you are using corresponds to the Class#getSimpleName() which is probably not what you want here.

like image 133
Amokrane Chentir Avatar answered Jan 01 '26 13:01

Amokrane Chentir