ayush gupta
ayush gupta

Reputation: 627

Change Settings.Global.AUTO_TIME to always be 1

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.

Upvotes: 2

Views: 5066

Answers (2)

Daniel
Daniel

Reputation: 596

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

Upvotes: 1

ashkhn
ashkhn

Reputation: 1620

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.

Upvotes: 0

Related Questions