2

I want Android apps to believe that Automatic Time Zone is set to ON even though it actually is not. I read that Automatic Time Zone is detected using Settings.Global.AUTO_TIME variable, which returns value 1 if Automatic Time Zone is ON and value 0 if Automatic Time Zone is OFF. This variable is read using the below line

Settings.Global.getInt(getContentResolver(), Settings.Global.AUTO_TIME)

I read that this variable can only be set by a System App. Is there a way to change this variable somehow? Can I write an application that allows me to change this variable? Can this be done by rooting the android device.

ayush gupta
  • 607
  • 1
  • 6
  • 14

2 Answers2

1

The most commonly suggested solution is to use Settings.Global.putInt(getContentResolver(), Settings.Global.AUTO_TIME, 1). You will end up seeing this message in logcat, though:

Setting auto_time has moved from android.provider.Settings.System to android.provider.Settings.Global, value is unchanged.

According to the Android documentation, apps cannot modify these settings.

https://developer.android.com/reference/android/provider/Settings.Global

Global system settings, containing preferences that always apply identically to all defined users. Applications can read these but are not allowed to write; like the "Secure" settings, these are for preferences that the user must explicitly modify through the system UI or specialized APIs for those values.

These settings are read-only unless your app is installed as a system app or is signed by the manufacturer of the device that it is running on.

Most people end up rooting their device but there is an alternative. Device Owner apps can modify global system settings through a different API.

https://developer.android.com/reference/android/app/admin/DevicePolicyManager#setGlobalSetting(android.content.ComponentName,%20java.lang.String,%20java.lang.String)

You would do it like so:

DevicePolicyManager dpm = (DevicePolicyManager) getApplicationContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName component = new ComponentName(getApplicationContext(), DeviceAdminReceiver.class);
dpm.setGlobalSetting(component, Settings.Global.AUTO_TIME, enable ? "1" : "0");

This alternative is mostly suitable for homebrew apps and proprietary Android-based products because the device cannot have user accounts. Play Store will not install apps as Device Owner, it must be done manually.

Here is how to do it with ADB:

https://developer.android.com/work/dpc/dedicated-devices/cookbook#dev-setup

Daniel
  • 556
  • 3
  • 5
0

You can use Settings.Global.putInt(getContentResolver(), Settings.Global.AUTO_TIME, 1) to set the value from your app.

You need to have the WRITE_SETTINGS permission though.

ashkhn
  • 1,642
  • 1
  • 13
  • 18
  • How to get write permissions? Isn't that available only to system apps? – ayush gupta Apr 24 '17 at 05:54
  • You need to create an app and request that permission. User apps can have the permission as well. – ashkhn Apr 24 '17 at 06:31
  • That piece of code always returns false on user apps, even if `WRITE_SETTINGS` permission is granted, thus it doesn't work. Source: https://developer.android.com/reference/android/provider/Settings.Global.html "Global system settings, containing preferences that always apply identically to all defined users. Applications can read these but are not allowed to write" – Cris Jan 29 '18 at 09:18