I know that the question about turning on/off GPS programatically on android has been discussed many times, and the answer is always the same:
"You can't for security/privacy reasons, you have to forward to location preferences screen and let the user enable/disable it."
I understand that, however I recently bought Tasker from the market and, among many other things that you can accomplish with it, you can set rules to auto-enable GPS on entering pre-determined applications and disable it on exit (see here for the tutorial on how to do it, and it just works!) and this app can't be signed with the firmware signing key as it works on many android versions and different devices and you don't even need to be rooted.
I would like to do this in my app. Of course, I don't want to blow up the users privacy, so I would first ask the user if he wants to turn it on automatically with the typical "remember my decision" checkbox and if he answers yes, enable it.
Does anybody have any idea or clue on how Tasker achieves this?
This code works on ROOTED phones:
For turning off GPS you can use this command instead
You can also toggle network accuracy using the following commands: for turning on use:
and for turning off you can use:
All these answers are not allowed now. Here is the correct one:
For all those still looking for the Answer:
Here is how OLA Cabs and other such apps are doing it.
Add this in your onCreate
These are the implmented methods:
Here is the Android Documentation for the same.
This is to help other guys if they are still struggling:
Edit: Adding Irfan Raza's comment for more help.
Instead of using intent Settings.ACTION_LOCATION_SOURCE_SETTINGS you can directly able to show pop up in your app like Google Map & on Gps on click of ok button their is no need to redirect to setting simply you need to use my code as
}
Note : This line of code automatic open the dialog box if Location is not on. This piece of line is used in Google Map also
You just need to remove the
LocationListener
fromLocationManager
An answer was developed in another question, but it was closed, and I'd like the community to try it out as well.
See this comment
This solution would require the
WRITE_SETTINGS
andWRITE_SECURE_SETTINGS
permissions.This is the best solution provided by
Google Developers
. Simply call this method in onResume of onCreate after initializingGoogleApiClient
.Note : This line of code automatic open the dialog box if
Location
is not on. This piece of line is used in Google Map also