I am going to build an App and once I have taken permission for location and it is on now if I off location manually then how it would be on in App automatically. Is there any way to location on automatically by using App once I have given permission.
Asked
Active
Viewed 369 times
1
-
1Use fused location API. Check here [link](https://developer.android.com/training/location/receive-location-updates.html) – Ashish M Mar 03 '17 at 07:04
-
Hey M. Ashish you did not get my point actually I have already got the location but my problem is how can i on location automatically again and again whenever I start my application if my app already have taken permission first time after installation. – Ajay Sharma Mar 03 '17 at 07:10
-
1Thats not a good idea. You just have to check whether gps is on or not, if its off show a proper dialog to user to turn on the gps. Don't turn on gps programatically. – Ashish M Mar 03 '17 at 07:31
-
From Android kitkat if you try to enable gps programmatically then you will get exception "java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE" – Manish Jain Mar 03 '17 at 07:34
-
You have to do like this [link](https://developer.android.com/training/location/change-location-settings.html) – Ashish M Mar 03 '17 at 07:39
-
I know but if I have already taken permission runtime once then it should not ask again and again after reopen app actually when I closed my app and turned off my gps now again going inside app then it's again asking for location but I don't want this I want if I have given permissions for app then it should not ask for permission. – Ajay Sharma Mar 03 '17 at 07:40
-
This is not permission thing, runtime permission should be asked once on First launch of APP. This is for checking the status of GPS. You have to made user to turnOn the GPS, – Ashish M Mar 03 '17 at 07:47
1 Answers
0
If what you want is to automatically turn on GPS, you can refer here. Look for answer given by Mr.Akshat .
If you need to check whether you are able to access the location services, use this code:
public boolean canGetLocation() {
boolean result = true;
LocationManager lm;
boolean gps_enabled = false;
boolean network_enabled = false;
if (lm == null)
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// exceptions will be thrown if provider is not permitted.
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = lm
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
if (gps_enabled == false || network_enabled == false) {
result = false;
} else {
result = true;
}
return result;
}