0

I'm developing an app where I have a Toggle Button to turn on/off GPS. I went through this link 1st Reference and also this 2nd Reference

Since the answers are controversial I couldn't figure out. I use DU BATTERY SAVER and it has a widget where I can turn on/off GPS. The same feature I want to use.

Community
  • 1
  • 1
SilleBille
  • 605
  • 5
  • 21

2 Answers2

0

how to programatically set GPS on/off in 4.0 and above

This should not be possible, for privacy reasons.

I use DU BATTERY SAVER and it has a widget where I can turn on/off GPS

Fortunately, they do not appear to have the feature that you describe, at least in the free edition for unrooted devices.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

You can try below code snipp

public void turnGPSOn()
{
     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
     intent.putExtra("enabled", true);
     this.ctx.sendBroadcast(intent);

    String provider = Settings.Secure.getString(ctx.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")); 
        this.ctx.sendBroadcast(poke);


    }
}
// automatic turn off the gps
public void turnGPSOff()
{
    String provider = Settings.Secure.getString(ctx.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")); 
        this.ctx.sendBroadcast(poke);
    }
}
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
  • Unfortunately, this code makes my phone to search for GPS satellites. I couldn't switch it off. I had to restart my phone – SilleBille Jan 15 '14 at 17:28