How to check if GPS works in Android app?

I would like to be able to check if the GPS phone is working or not (in the application code). Right now, I'm just trying to directly access the GPS location without

if ( GPS_is_working ) {

}

I have a problem right now, because when I get access to GPS, when it does not work, a forced close dialog appears and my activity is turned off.

What is the code for this?

+3
source share
1 answer

You can call this method:

public boolean isGpsEnabled(Context context) {
    LocationManager locationManager = ((LocationManager)context.getSystemService(Context.LOCATION_SERVICE));
    List<String> accessibleProviders = locationManager.getProviders(true);
    return accessibleProviders != null && accessibleProviders.size() > 0;
}

and just

if(isGpsEnabled(context)) { /*go ahead knock yourself out :) */}
+2
source

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


All Articles