Reputation:
My application needs the GPS to be on, is there any way to check whether GPS is currently enabled or not and if not , then how to enable. I am using android 2.3
Upvotes: 3
Views: 4864
Reputation: 3017
You can do it this way..check it out
private void turnGPSOn()
{
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps"))
{ //if gps is off
final Intent enablegps = new Intent();
enablegps.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
enablegps.addCategory(Intent.CATEGORY_ALTERNATIVE);
enablegps.setData(Uri.parse("3"));
sendBroadcast(enablegps);
}
}
private void turnGPSOff()
{
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps"))
{ //if gps is on
final Intent disablegps = new Intent();
disablegps.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
disablegps.addCategory(Intent.CATEGORY_ALTERNATIVE);
disablegps.setData(Uri.parse("3"));
sendBroadcast(disablegps);
}
}
Upvotes: 0
Reputation: 139
Android does not allow you to do that. The best thing you can do is checking if the GPS is enabled and if it's not, ask the user to activate it.
Here you can see how to know if the GPS is enabled: How do I find out if the GPS of an Android device is enabled
Upvotes: 4