8

I'm developing a tracking application and I need to prevent users from turning off the basic sensors used to determine the location. I can not modify the devices ROM or have root access (or at least it would be very desirable to had not), but I thought of using the Device Administration API to perform these functions through the Profile Owner or Device Owner modes. I'm basically looking for a method to block these functions in Android settings.

I'm unsure about whether this is possible and how to do it, I have not found examples in GitHub for applications that have implemented this. Could anyone give me a light, some example or specific documentation?

I tried to follow these three documentations, without success in finding a solution to this specific feature:

https://source.android.com/devices/tech/admin https://developer.android.com/guide/topics/admin/device-admin https://developers.google.com/android/management/introduction

This is an excerpt from what I've been trying:

setUserRestriction(UserManager.DISALLOW_CONFIG_WIFI, true);
setUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS, active);
setUserRestriction(UserManager.DISALLOW_CONFIG_BLUETOOTH, active);

private void setUserRestriction(String restriction, boolean disallow){
    if (disallow) {
        mDevicePolicyManager.addUserRestriction(mAdminComponentName, restriction);
    } else {
        mDevicePolicyManager.clearUserRestriction(mAdminComponentName,
restriction);
    }
}

DISALLOW_CONFIG_BLUETOOTH Added in API level 18 public static final String DISALLOW_CONFIG_BLUETOOTH

Specifies if a user is disallowed from configuring bluetooth. This does not restrict the user from turning bluetooth on or off. The default value is false.

This restriction doesn't prevent the user from using bluetooth. For disallowing usage of bluetooth completely on the device, use DISALLOW_BLUETOOTH.

This restriction has no effect in a managed profile.

Key for user restrictions.

Type: Boolean

Lucas Cleto
  • 195
  • 1
  • 16
  • 1
    You cannot prevent them from turning stuff off, the only thing you can do is when your app loads tell the user you need these things on (and why) and not let them use the app until they do – tyczj May 29 '19 at 16:44
  • you are going in wrong way (don't force users) . maybe you need to change your strategy . you can observe GPS status and doing proper action in each cases ... – Kapta May 29 '19 at 18:06
  • It's a corporate app. So, the company may use a MDM to deploy the app and control devices. If it so, they probably can avoid some settings to be disabled. – cesarmarch Jun 04 '19 at 18:18
  • For GPS you can try the solution mentioned in below link [How can I enable or disable the GPS programmatically on Android?](https://stackoverflow.com/questions/4721449/how-can-i-enable-or-disable-the-gps-programmatically-on-android) – Rajiv Ranjan Jun 07 '19 at 11:08

4 Answers4

6

You cannot prevent them from turning GPS, WIFI and Bluetooth off. What you can do is have an implementation as below or use this library.

https://github.com/KI-labs/gps-permission-checks-livedata

SRBhagwat
  • 1,491
  • 2
  • 16
  • 21
  • Oh, that's really going to be a problem for me. Can I do this at least if I have root access? – Lucas Cleto May 29 '19 at 17:47
  • Yeah but that is not easy i believe. What is the usecase you are trying to solve? – SRBhagwat May 29 '19 at 17:56
  • It is an application for corporative use and I need to prevent users from circumventing it. – Lucas Cleto May 29 '19 at 18:04
  • 1
    Device owner lets your app control off/on when needed. But i don't think you can block users from manually turning it off. Do give it a try and let us know your findings. – SRBhagwat May 30 '19 at 01:50
4

You can't, obviously for security reasons. If you want to achive something like that you'll probably need to modify the devices ROM. You should create a BroadcastReceiver and keep tracking Internet and Bluetooth connection changes, than you can properly handle it when user disconnect them pausing the service, showing a dialog, finishing the application or whatever you need to do.
It would be pretty weird if an app could have some control of user settings, imagine if you install an app, then suddently you can't disable wi-fi anymore until you unistall it. You can't do that for a good reason

Shermano
  • 1,100
  • 8
  • 31
1

Preventing bluetooth/wifi disconnection will also prevent usage of aircraft mode, that is a security issue bounded in the ROM and not overridable. As suggested above your option is to monitor for wifi/bluetooth/gps deactivations and prompt the user with an alert. By the way, GPS is not affected by aircraft mode, as it's a pure receiver and doesn't make active transmissions. In that case GPS will be always active and collecting informations (if active and the phone is not in power save mode, aka relying on wifi location). I suggest you to check if the user activated aircraft mode, in order to be less annoying with your alerts (air mode is mandatory in same situations, and should be considered "legal" by your application, and maybe less critical than an user voluntary disconnection

0

In simple words, You cannot, but you can listen to when wifi is enabled/connected, and you can prompt a dialog stating the reason.
This way it gives the user a more concise grip on what needs to be done.

Just a suggestion

hemen
  • 1,460
  • 2
  • 16
  • 35