Returns the current enabled/disabled status of the given provider.
If the user has enabled this provider in the Settings menu, true is returned otherwise false is returned
Callers should instead use LOCATION_MODE unless they depend on provider-specific APIs such as requestLocationUpdates(String, long, float, LocationListener).
Parameters: provider the name of the provider
Returns: true if the provider exists and is enabled
Throws:
IllegalArgumentException - if provider is null
SecurityException - if no suitable permission is present
1. In the MainActivity.java file, Get a Reference to LocationManager
2. Get the Name of the GPS Location Provider named myProvider.
3. In the onCreate method, add the following code. This will test if the GPS is enabled or disabled. You can optionally Add a Toast.
try {
if (myLocationManager.isProviderEnabled(myProvider)) {
//Toast.makeText(this, "GPS is Enabled", Toast.LENGTH_LONG).show();
} else {
//Toast.makeText(this, "GPS is Disabled", Toast.LENGTH_LONG).show();
}
} catch( IllegalArgumentException e) {
e.printStackTrace();
//Toast.makeText(this, "isProviderEnabled - IllegalArgumentException", Toast.LENGTH_LONG).show();
} catch( SecurityException e) {
e.printStackTrace();
//Toast.makeText(this, "isProviderEnabled - SecurityException", Toast.LENGTH_LONG).show();
}
4. Compile and run!
Resources:
Other Resources:
Learning Android Application Programming: A Hands-On Guide to Building Android Applications