2

I'd like to know if it possible to enable and disable Location Services programatically in Android 4.0?

I've found several approaches how to do this for previous versions of Android (for instance, this is the most popular link). But these approach does not work for Android ICS.

Also, I understand that an application should not do this but, for instance, default widget does this.

Can anybody clarify if it is possible and how to achieve this?

Community
  • 1
  • 1
Yury
  • 20,618
  • 7
  • 58
  • 86

3 Answers3

2

You can enable and disable gps device but users must confirm in setting widget. The solution in this topic.

Community
  • 1
  • 1
tho nguyen
  • 76
  • 5
  • Try to read the question more carefully. It's not a problem to redirect a user to the page, where she can disable it manually. – Yury Jan 18 '13 at 09:13
  • Thank Yury! I have researched it but I can't programming enable with normal permission. I am very sorry because my English is not very well! – tho nguyen Jan 22 '13 at 02:44
2

In a tiny app I have developed, I turn off the gps by just disabling the location on the MyLocationOverlay. Of course, this will work only if you are using maps from Google as that class belongs to the Google API's.

myLocationOverlay.disableMyLocation();

I place the line on the onStop() method and only if it is currently enabled. And every time my app goes to the background, the GPS turns automatically off.

Hope it helps.

Luis Ollero
  • 383
  • 1
  • 2
  • 8
  • No, I want one method to enable GPS and one to disable. My application should work without additional overlays. The idea of the app is the following: during the start of the application I want to check if GPS is enabled (I need GPS provider) and if it isn't I want to enable it. At the end, I want to disable GPS (if it has been disabled before the application start). But thank you for your answer! – Yury Feb 09 '12 at 16:35
1
LocationManager L = (LocationManager) getSystemService(LOCATION_SERVICE); 

if (!L.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
     Intent I = new Intent( 
                android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
        startActivity(I);  
}
Sujith Ks
  • 360
  • 2
  • 10