Boot设备时,如果无法连接到网络,如何在android事物上设置当前时间?
When Boot device, if can not connect to the network, how to set current time on android things?
我在 Raspberry pi 上使用 Android-Things
。
启动设备时。如果连接到网络,
我试试TimeManager
API.
private void setupTimeZone(String timeZoneName) {
TimeManager timeManager = TimeManager.getInstance();
timeManager.setTimeFormat(TimeManager.FORMAT_24);
timeManager.setTimeZone(timeZoneName);
}
setupTimeZone("Asia/Seoul");
如果网络连接到 Raspberry pi
设置时间没问题。
但我的问题只是在启动设备时,没有连接网络。
如果设备未连接网络。基本上时间设置为Jan 1, 2009 09:00
要更改默认日期,需要修改哪些文件?
谢谢。
设置时间可以使用TimeManager.setTime()
方法:
To control device settings using TimeManager, first request the
permission com.google.android.things.permission.SET_TIME
in your
AndroidManifest.xml, then obtain an instance of the class and set the
properties appropriate for your app.
TimeManager timeManager = TimeManager.getInstance();
// Use 24-hour time
timeManager.setTimeFormat(TimeManager.FORMAT_24);
// Set time zone to Eastern Standard Time
timeManager.setTimeZone("America/New_York");
// Set clock time to noon
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 12);
long timeStamp = calendar.getTimeInMillis();
timeManager.setTime(timeStamp);
但是Raspberry Pi 3 has no built-in Real Time Clock (RTC) and there is no possibilities for it to get actual current time without network connection or using external battery-backed RTC module like DS1307 or DS3231 or many others (also take a look at this manual). Often RTC modules use I2C interface, so you should connect RTC module to your board, initially (when your board was connected to network and current time was known) set actual time to it via I2C, and then, on boot get current time from RTC module and set it to Android Things system like in example above. How to control DS3231 RTC via I2C you can find here. Internals of User space driver for that example you can find there.
您还可以从 GPS (e.g. from RMC
sentence) and GSM (e.g. AT+CLTS
command) modules connected via UART.
获取当前时间
我在 Raspberry pi 上使用 Android-Things
。
启动设备时。如果连接到网络,
我试试TimeManager
API.
private void setupTimeZone(String timeZoneName) {
TimeManager timeManager = TimeManager.getInstance();
timeManager.setTimeFormat(TimeManager.FORMAT_24);
timeManager.setTimeZone(timeZoneName);
}
setupTimeZone("Asia/Seoul");
如果网络连接到 Raspberry pi 设置时间没问题。
但我的问题只是在启动设备时,没有连接网络。
如果设备未连接网络。基本上时间设置为Jan 1, 2009 09:00
要更改默认日期,需要修改哪些文件?
谢谢。
设置时间可以使用TimeManager.setTime()
方法:
To control device settings using TimeManager, first request the permission
com.google.android.things.permission.SET_TIME
in your AndroidManifest.xml, then obtain an instance of the class and set the properties appropriate for your app.TimeManager timeManager = TimeManager.getInstance(); // Use 24-hour time timeManager.setTimeFormat(TimeManager.FORMAT_24); // Set time zone to Eastern Standard Time timeManager.setTimeZone("America/New_York"); // Set clock time to noon Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.HOUR_OF_DAY, 12); long timeStamp = calendar.getTimeInMillis(); timeManager.setTime(timeStamp);
但是Raspberry Pi 3 has no built-in Real Time Clock (RTC) and there is no possibilities for it to get actual current time without network connection or using external battery-backed RTC module like DS1307 or DS3231 or many others (also take a look at this manual). Often RTC modules use I2C interface, so you should connect RTC module to your board, initially (when your board was connected to network and current time was known) set actual time to it via I2C, and then, on boot get current time from RTC module and set it to Android Things system like in example above. How to control DS3231 RTC via I2C you can find here. Internals of User space driver for that example you can find there.
您还可以从 GPS (e.g. from RMC
sentence) and GSM (e.g. AT+CLTS
command) modules connected via UART.