tags:

views:

35

answers:

1

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?

+1  A: 

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 :) */}
MahdeTo
It's definitely a perfect function, but for some reason, when I try it in my app, I'm still getting an app-crash (i.e. force close dialogue popping up)...Am I supposed to be using <ClassName.this> as the context? I am trying to test this function by turning off the GPS (in phone's settings), then I call this function - and I have an <else { } > statement that's supposed to display a Toast, but I just keep getting the force-close... Any idea of anything else I can try? Thanks, btw.
Yasir Malang