Monday, September 5, 2011

Enable or Disable GPS in Android through Programatically

To Enable GPS :
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
To Disable GPS :

 Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);

12 comments:

  1. Replies
    1. private void turnGPSOn()
      {
      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);
      }
      }

      Delete
  2. I cant understand these two lines Vivek!

    poke.setData(Uri.parse("3"));
    sendBroadcast(poke);

    ReplyDelete
  3. I wouldn't rely on this method as it apparently exploits a known bug which has since been fixed

    There is a good explanation can be found here:
    http://stackoverflow.com/a/5305835

    ReplyDelete
  4. Is it working in ICS ?? I am trying in my ICS but no use..may be work in 2.3 and below versions.

    ReplyDelete
  5. This is not working for 3.0 and above version in android

    ReplyDelete
  6. What is the alternative solution for doing same for the version above kitkat

    ReplyDelete