Set Android Things time zone

*爱你&永不变心* 提交于 2019-12-08 01:52:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!