Enable or Disable GPS in Android through Programatically
To Enable GPS :
Intent intent = new Intent("android.location.GPS_To Disable GPS :ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);
NOT WORKING
ReplyDeleteprivate void turnGPSOn()
Delete{
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps"))
{ //if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
}
private void turnGPSOff()
{
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps"))
{ //if gps is enabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
}
Thank u lot!!!
ReplyDeleteI cant understand these two lines Vivek!
ReplyDeletepoke.setData(Uri.parse("3"));
sendBroadcast(poke);
I wouldn't rely on this method as it apparently exploits a known bug which has since been fixed
ReplyDeleteThere is a good explanation can be found here:
http://stackoverflow.com/a/5305835
Is it working in ICS ?? I am trying in my ICS but no use..may be work in 2.3 and below versions.
ReplyDeletelOl
ReplyDeleteThis is not working for 3.0 and above version in android
ReplyDeleteTHanks worked
ReplyDeleteNOT WORKING ON LOLLIPOP
ReplyDeleteWhat is the alternative solution for doing same for the version above kitkat
ReplyDeleteIts not working in LOLIPOP
ReplyDeleteNot Working
ReplyDelete