2

Can I turn ON/OFF GPS in an application? As it drains so much of battery, can I turn it OFF for some time? OR there is better solution for this .....

user533844
  • 2,053
  • 7
  • 36
  • 43

6 Answers6

3

You ask the user to do this, (using a toast maybe), start this activity

if(!LocationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER ))
{
    Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS );
    startActivity(myIntent);
}
Reno
  • 33,594
  • 11
  • 89
  • 102
2

Try this...

   locationManager.removeUpdates(myLocationListener); locationManager = null;
Aravindhan
  • 15,608
  • 10
  • 56
  • 71
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);
    }
}
Ashok Domadiya
  • 1,124
  • 13
  • 31
0

You can't turn it on/off yourself. You can however prompt the user to turn it off/on: How to programmatically enable GPS in Android Cupcake

Community
  • 1
  • 1
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
0

I think as a security measure the android system won't allow you to turn on and off the gps for the user(as of android 2.0 I think). You can either stop getting updates and that supposed to stop using the gps (unless other application use it), or ask the user to switch it off.

Raz
  • 8,918
  • 3
  • 27
  • 40
0

You can stop listening for GPS events in your application (if your application uses GPS). for instance, using LocationManager.removeUpdates.

If you want to "turn off" the GPS at OS level, I think it is not possible programmatically.

Mister Smith
  • 27,417
  • 21
  • 110
  • 193