Reputation: 480
I want to put global time zone programatically in Android. The ADB way is like this:
adb shell settings put global time_zone Europe/Stockholm
When I get the time zone it works fine:
adb shell settings get global time_zone
The problem is when I want to do this in Android Studio:
public void setTimeZone(){
try {
Runtime.getRuntime().exec("settings put global time_zone Europe/Stockholm");
}catch (IOException e) {
e.printStackTrace();
}
}
There is no error but the time zone is not set.
Any suggestions please? Thank you.
Upvotes: 0
Views: 3072
Reputation: 814
Why are you trying to change it via adb shell? Try this:
AlarmManager am = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
am.setTimeZone("Europe/Stockholm");
You will need to add this permission to your Androidmanifest.xml
<uses-permission android:name="android.permission.SET_TIME_ZONE"/>
I hope this helps you.
With a rooted phone you can try this:
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();
}
Upvotes: 2