Programmatically disable GPS in android >= 7 , not root or system app - android

I am currently developing an application that requires blocking or disabling the use of GPS on the device, I know that there are functions from EMM with Device Police Manager, root, or system application. But the requirement is to apply it to a common user application, do you know any way to do it?
PD: "It should be noted that the idea is to disable without turning it off or on by the user, that is, asking the user to turn it off does not work, the idea is to disable the option to turn the GPS on or off"

Related

How to programmatically turn on GPS on Android

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.

Android show android natice pop up settings/cancel to enable location service

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.

How to turn on GPS automatically whenever off in android?

I am developing an android app and I need to turn on gps automatically if gps is off and if users try to turn off gps then too I need to turn gps on. In short users must not be able to turn of gps. But I have read that this is possible only in rooted phones. But this app need to be used in unrooted phones. Is there anyway to turn gps on automatically? and never allow to turn off?
Fortunately, no, for obvious privacy reasons. You will find various script-kiddie hacks that may work on certain devices or certain older Android versions, but that is it.

Using Android internal API to turn on GPS

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.

find out if power saving mode enabled - Android SDK

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.

Categories

Resources