Yasir Malang
Yasir Malang

Reputation: 835

How to test to see if GPS is working in Android App?

I would like to be able to test to see if the Phone's GPS is working or not (in the app's code). Right now, I am just trying to directly access the GPS location without an

if ( GPS_is_working ) {

}

I am having a problem right now since when I access the GPS when it is not working, the force-close dialogue shows up and my activity is shut down.

What is the code to do this?

Upvotes: 3

Views: 956

Answers (1)

MahdeTo
MahdeTo

Reputation: 11194

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 :) */}

Upvotes: 2

Related Questions