"adb shell settings put global time_zone" 在 Android 中无法以编程方式工作

"adb shell settings put global time_zone" doesn't work programatically in Android

我想以编程方式将全球时区放入 Android。 ADB方式是这样的:

adb shell settings put global time_zone Europe/Stockholm

当我得到时区时,它工作正常:

adb shell settings get global time_zone

问题是当我想在 Android Studio 中执行此操作时:

public void setTimeZone(){

    try {
        Runtime.getRuntime().exec("settings put global time_zone Europe/Stockholm");

    }catch (IOException e) {

        e.printStackTrace();
    }

}

没有错误,但没有设置时区。

有什么建议吗?谢谢。

您为什么要尝试通过 adb shell 更改它?试试这个:

AlarmManager am = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
am.setTimeZone("Europe/Stockholm");

您需要将此权限添加到您的 Androidmanifest.xml

<uses-permission android:name="android.permission.SET_TIME_ZONE"/>

希望对您有所帮助。

有了 root phone 你可以试试这个:

   try {
        Process su = Runtime.getRuntime().exec("su");
        DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

        outputStream.writeBytes("settings put global time_zone Europe/Stockholm\n");
        outputStream.flush();

        outputStream.writeBytes("exit\n");
        outputStream.flush();
        su.waitFor();

    } catch (Exception e) {

        e.printStackTrace();
    }