I know it's not supposed to be possible to turn on localization without having to go into the settings, but is there any way to get permission from the user to turn on the localization at a specific time of the day?
Asked
Active
Viewed 62 times
1 Answers
1
There is two separate things as turning on gps
and getting a gps fix
. so you have to get permission from user or request user to turn on gps when it is turned off, but you dont need to ask when the gps is on and you need a location. Keeping the gps turned on whole time does not drain the battery as much. but if any program is requesting for location every time it will.
So in your app what you can do is-
- Keep the Gps turned on everytime. but do not request for location.
- and also keep in mind to unregister the LocationListener or googleApiClient when not in use so that it will not drain the device battery.
Code Example:
To check and turn on gps:
private void CheckEnableGPS(){
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.equals("")){
//GPS Enabled
Toast.makeText(AndroidEnableGPS.this, "GPS Enabled: " + provider,
Toast.LENGTH_LONG).show();
}else{
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivity(intent);
}
}
To request for current location:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
@Override
public void onLocationChanged(Location location) {
Log.i("log","Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
}
you can also use Google Fused API
Additional Resources:

Sagar Nayak
- 2,138
- 2
- 19
- 52
-
So I can keep gps on all the time and location off, and when I need location I can just get it without having the user to click a dialog or go to settings? – John Sardinha Jun 29 '16 at 16:29
-
yes you can. you just tell user that your app will not use gps but it needs to be kept on all the time. and prompt the same message when ever user tries to turn the gps off. keep a listener to listen to the gps change event. – Sagar Nayak Jun 29 '16 at 16:33
-
But how is it possible to keep the gps turned on without draining battery ? – John Sardinha Jun 29 '16 at 16:34
-
tuning on gps does not drain your battery mate. it drains battery when you turn on the gps and request for location. – Sagar Nayak Jun 29 '16 at 16:35
-
Alright, can you provide some code on how to turn on the gps, and location please? – John Sardinha Jun 29 '16 at 16:38
-
i have edited the answer . please consider accepting it as right answer if you like. – Sagar Nayak Jun 29 '16 at 16:54
-
Thank you. When the settings open, what does the user have to click on? – John Sardinha Jun 29 '16 at 17:05
-
user have to turn on the location settings by clicking the toggle button in the settings. please consider accepting it as right answer if you like. – Sagar Nayak Jun 30 '16 at 03:41
-
I will once I fully get it. I know I may sound dense, but let me make sure this is exactly how it is: the gps and the location are two separate things, I can keep the user's gps turned on all the time without much battery drainage and with that, I can turn on the location at a specific time without the user visiting the settings every time? – John Sardinha Jun 30 '16 at 04:20
-
hey . i suggest you read about this thing in android developer website and the resources i have provided to you in the answers. i dont think you have enough knowledge to work in this thing . so please read. – Sagar Nayak Jun 30 '16 at 04:41