I have survey some article on stackoverflow.
It seems android version above 4.0 is not able to turn on/off GPS programmatically.
Is there anything changed now? Or is this still not possible?
You can't turn it on/off programatically due to security reasons. According to Change location settings docs:
If your app needs to request location or receive permission updates, the device needs to enable the appropriate system settings, such as GPS or Wi-Fi scanning. Rather than directly enabling services such as the device's GPS, your app specifies the required level of accuracy/power consumption and desired update interval, and the device automatically makes the appropriate changes to system settings.
So, the best thing to do is, if the user has disabled the GPS, then for better UX you can show a prompt and redirect the user to the Location activity like this:
MainActivity.Instance.StartActivity(new Intent(ActionLocationSourceSettings));
Where MainActivity.Instance is your current Activity.
Related
I want to enable/disable location services for my app but not have to prompt the user to enable it if it's turned off. All the examples I have seen require some dialog to be shown. This seems kind of senseless given that the user has already granted permission to use location services.
I want to enable/disable location services for my app but not have to prompt the user to enable it if it's turned off.
Fortunately, that is not possible on ordinary devices, for obvious privacy reasons, barring some bug in Android (or specific device models). System apps might be able to do this, and this should be possible on rooted devices, though I do not have the details for either scenario.
This seems kind of senseless given that the user has already granted permission to use location services.
Prior to Android 6.0, users did not have the ability on ordinary Android devices to control permissions individually. As a result, they might disable location services, just to be able to use apps that happen to request locations.
Even on Android 6.0+, just because the user granted your app permission to use locations does not mean that the user wants location data to be available all the time. They might only want locations to be available to apps at certain times (e.g., while travelling and needing location-related information more). Or, as a commenter noted, the user might keep locations disabled for power reasons, more so than privacy.
if the app needs control of the location service and the user is made fully aware that the app needs to turn it on/off when it is running
That would need to be a separate capability with a separate permission (e.g., some MANAGE_LOCATION_PROVIDERS permission), or possibly be part of the device admin/device owner APIs. You are certainly welcome to file a feature request for this.
No prompt should be necessary if the permission has been granted
Permissions to access location data do not imply permission to override the user's enabled/disabled setting for location providers. If it did, there would be no point in having location providers be able to be disabled.
I am working on an android application that uses geo location services, when the user accesses such service if location access is disabled on the device i want to call android native pop up that guides user to enabled location access. Is it possible to do this ?
I noticed there's even better way to achieve good location settings, without "guiding" user to turn them on, mentioned in this question:
Enabling Location with mode High Accuracy or Battery saving without user needing to visit Settings
Since Google Play Services 7.0 you can ask system to offer user turning the settings on with one touch. It's how Google Maps are doing it.
Is there any hidden (internal) API function for turning on/off the GPS? Is it a valid solution (legally and safely) to use android internal API's? If it is not valid, how can I obtain a license/signature for using those API's?
I have already tried the methods using SDK/NDK and I understood that it is not possible to turn GPS on with the SDK/NDK (I know that we can bring up the settings screen). I want to know whether there is any hidden/internal API's for doing that. Also I am not interested in using any vulnerabilities in Android as a solution.
You need root privileges. Mandatory. Period.
And yes there is API to enable/disable GPS. But the application must receive SuperAdmin privileges. To obtain it either root or custom ROM required.
Sample: https://github.com/sms2000/GPSToggler
There is no hidden API for controlling the GPS. There is no valid solution.
(Like you mentioned), the correct thing to do is ask the user to turn it on. If an app could override the users option, there would be no point in it being an option and this would be a huge security rick.
If the user chooses to keep the GPS off then your apps should do the same thing it would do if the GPS was on but couldn't get a lock. Not respecting their wished to keep it off would be malicious even if you don't plan on doing anything morally wrong with the data.
If you have some client that wants their employees to not be able to turn off the GPS, then you need to give them phones with custom roms.
If you are trying to make a security app that reports location you can use wifi location and last known location too in addition to GPS.
I'm working on an android app and i need to know the location of android device. All the methods I tried need GPS to be enabled. If it is not enabled, we can ask the user to turn it on by opening settings intent.
Is there any way to enable it via code, without even letting the user know.. and turn it off when the app has finished its job?
The app I'm working on will keep record of location of device. It will run in background and will note location every 15mins. To keep GPS enabled all the time will consume a lot of power. So i want to turn it on when i need and turn it off when i'm done, without asking the user.
For exact location, you need GPS to be enabled. But you can use NETWORK_PROVIDER, and get the latitude and longitude. This does not require user intervention. But, of course, its an approximate location but good enough to do location based searches.
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
locationListener);
For privacy reasons, that will never be possible without rooting the device.
You cannot get the user's location without consent.
Is there any way to enable it via code, without even letting the user know.. and turn it off when the app has finished its job?
Fortunately, no, for obvious privacy and security reasons.
So i want to turn it on when i need and turn it off when i'm done, without asking the user.
On and off are not the same as enabled and disabled.
Just because the user has allowed GPS to be enabled does not mean that it is drawing power. Only if an app has requested location data will the GPS radio be turned on. So, you cannot enable GPS (only users can), but you can arrange for GPS to be turned on if it is enabled. The user will still see an icon in the status bar when GPS is on.
How does the spy apps and all do that then?
They require that GPS be enabled by the user.
I have an app running on Android that uses only a webview to display a mobile web-application and at some point uses the device's GPS to obtain their position.
I have a custom ChromeWebClient etc and it obtains their position nicely except for on devices where the Power Saving Mode is enabled?
Is there anyway in the SDK/API for me to be able to determine if the user has this enabled and to advise them accordingly?
I can't find anything in the docs so i am assuming not but worth a shot,
Cheers,
Lee
After reeding the comments
In my experience Samsung, as well as HTC, is one of the manufacturers that modify Android OS in most unpredictable ways. They add new functions and modes, like 4G switching launcher widgets and "power saving mode". They modify permission requirements for documented SDK methods, i.e. to switch on bluetooth on a Samsung device your app needs to have and additional android.permission.BLUETOOTH permission, while stock Android only needs android.permission.BLUETOOTH_ADMIN. And there's more.
Long story short, as #RaghavSood pointed out, "power saving mode" is not an official AOSP feature and there are no ways to detect it via official SDK. There is a possible way how you can work around it though. I believe it is most likely that your app misbehaves in power saving mode because this mode turns off GPS. You can confirm that by configuring power saving mode in settings to disable GPS disabling(can't phrase this better, sorry) - first link from google with steps. Then test the app. Does it work? If yes, then you've rootcaused the problem and now your job is to let the user know that your app won't run without GPS. You can put some code into your app to detect if GPS service is enabled an show an alert dialog if it isn't. The code in your activity can look something like this:
LocationManager lm = getSystemService(Context.LOCATION_SERVICE);
if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//Show a notification prompting user to switch on GPS.
}
You can be even more elaborate and make your app detect device manufacturer to show a custom message on all Samsung devices.