我希望 Android 应用相信自动时区已设置为开启,即使实际上并未开启。我读到使用 Settings.Global.AUTO_TIME 变量检测到自动时区,如果自动时区打开则返回值 1,如果自动时区关闭则返回值 0。 使用下面的行读取此变量
Settings.Global.getInt(getContentResolver(), Settings.Global.AUTO_TIME)
我读到这个变量只能由系统应用程序设置。有没有办法以某种方式更改此变量?我可以编写一个允许我更改此变量的应用程序吗?这可以通过对 android 设备进行 root 来完成吗?
最佳答案
最常建议的解决方案是使用 Settings.Global.putInt(getContentResolver(), Settings.Global.AUTO_TIME, 1)
。不过,您最终会在 logcat 中看到此消息:
Setting auto_time has moved from android.provider.Settings.System to android.provider.Settings.Global, value is unchanged.
根据 Android 文档,应用无法修改这些设置。
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.
这些设置是只读的,除非您的应用程序作为系统应用程序安装或由运行它的设备的制造商签名。
大多数人最终都会对他们的设备进行 Root,但还有另一种选择。设备所有者应用可以通过不同的 API 修改全局系统设置。
你会这样做:
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");
此替代方案主要适用于自制应用程序和专有的基于 Android 的产品,因为该设备无法拥有用户帐户。 Play 商店不会以设备所有者身份安装应用,必须手动完成。
以下是使用 ADB 的方法:
https://developer.android.com/work/dpc/dedicated-devices/cookbook#dev-setup
关于android - 将 Settings.Global.AUTO_TIME 更改为始终为 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43579224/