问题
After installing Android Things on a Raspberry Pi, time is not correct. My time zone is GMT+2, and using date +%Z
I see RPi's time zone is GMT. How can I set time zone?
回答1:
Update (based on Michal Harakal's comment):
Since Developer Preview 6 TimeManager class provides access to device settings related to time (NB! TimeManager
requires <uses-permission android:name="com.google.android.things.permission.SET_TIME" />
). You can use .setTimeZone() method of for time zone set:
private void setupTimeZone(String timeZoneName) {
TimeManager timeManager = TimeManager.getInstance();
timeManager.setTimeZone(timeZoneName);
}
where timeZoneName
is one of tz database time zones string, e.g. for Kyiv (GMT +2, DST +3):
setupTimeZone("Europe/Kiev");
Original answer:
You can set it programmatically from Application via AlarmManager.setTimeZone() like in this answer of Synesso:
AlarmManager am = (AlarmManager)getContext().getSystemService(Context.ALARM_SERVICE);
am.setTimeZone("Europe/Madrid");
with <uses-permission android:name="android.permission.SET_TIME_ZONE"/>
permission in AndroidManifest.xml
file.
List of TimeZone names.
回答2:
I use these codes
TimeManager timeManager = TimeManager.getInstance();
timeManager.setTimeFormat(TimeManager.FORMAT_24);
// Set time zone to Eastern Standard Time
//timeManager.setTimeZone("America/New_York");
timeManager.setTimeZone("GMT");
calendar.setTime(date);
long timeStamp = calendar.getTimeInMillis();
timeManager.setTime(timeStamp);
with this permission : com.google.android.things.permission.SET_TIME
来源:https://stackoverflow.com/questions/44537239/set-android-things-time-zone