0

I want enable and disable GPS. I want like Google map app, when run app, the app ask from me enable my location and when my app goes to background(pause state) gps disable, I mean i don't see gps icon in top of screen, and when again app goes to foreground, again my location enable . I did not use LocationOverlay . I write locationManager.removeUpdates(locationListener); in onPause() method, but when my app goes to background, i see gps on and enable and try detect my location!

I write enable location but i don't know how can disable gps and then hide gps icon!

Please help me

Thanks for advises.

user3209380
  • 169
  • 1
  • 2
  • 14

2 Answers2

1

Disable GPS programmatically on Android

 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);
        }
    }

To remove icon and turn off gps:

locationManager.removeUpdates(myLocationListener); 
locationManager = null;
Community
  • 1
  • 1
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • Thanks for answer. I write turnGPSOff() method without removerUpdates() and when app goes to background. gps turn off. what is difference between removeUpdates() and turnGPSOff() ? – user3209380 May 25 '14 at 06:21
  • and another question. I want when disable gps with second answer form your send link, my gps disable but on, and when i goes to app again, in onResume(), how can i detect gps disable that after i enable this and if gps is turn off, i turn on gps! I detect gps on and off wtih this code : (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) – user3209380 May 25 '14 at 06:33
0

Try those:

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);
}
}

Please note that you need to set proper app permissions.

Flash Thunder
  • 11,672
  • 8
  • 47
  • 91