[英]When Boot device, if can not connect to the network, how to set current time on android things?
I use Android-Things
on Raspberry pi. 我在Raspberry pi上使用
Android-Things
。 when booting the device. 启动设备时。 if connect to the network,
如果连接到网络,
I try TimeManager
API. 我尝试使用
TimeManager
API。
private void setupTimeZone(String timeZoneName) {
TimeManager timeManager = TimeManager.getInstance();
timeManager.setTimeFormat(TimeManager.FORMAT_24);
timeManager.setTimeZone(timeZoneName);
}
setupTimeZone("Asia/Seoul");
if network is connected to Raspberry pi set time is no problem. 如果网络连接到Raspberry pi设置时间没问题。
but my problem is only when booting the device, not connect network. 但我的问题只是在启动设备时,而不是连接网络。
if device not connect network. 如果设备没有连接网络。 basically the time is set as
Jan 1, 2009 09:00
基本上时间设定为
Jan 1, 2009 09:00
to change the default date, What files need to be modified? 要更改默认日期,需要修改哪些文件?
thanks. 谢谢。
For setting time you can use TimeManager.setTime()
method: 要设置时间,可以使用
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控制设备设置,请首先在AndroidManifest.xml中请求权限
com.google.android.things.permission.SET_TIME
,然后获取该类的实例并设置适合您的应用的属性。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);
but 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). 但是Raspberry Pi 3没有内置的实时时钟(RTC),没有网络连接或使用DS1307或DS3231等外部电池供电的RTC模块来获取实际当前时间的可能性(另外看看)在本手册中)。 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.
通常RTC模块使用I2C接口,因此您应该将RTC模块连接到电路板,最初(当电路板连接到网络并且当前时间已知时)通过I2C设置实际时间,然后,在启动时从RTC获取当前时间模块并将其设置为Android Things系统,如上例所示。 How to control DS3231 RTC via I2C you can find here .
如何通过I2C控制DS3231 RTC,你可以在这里找到。 Internals of User space driver for that example you can find there .
您可以在那里找到该示例的用户空间驱动程序的内部。
Also you can get current time from GPS (eg from RMC
sentence) and GSM (eg AT+CLTS
command) modules connected via UART . 您还可以通过UART连接GPS (例如来自
RMC
句子)和GSM (例如AT+CLTS
命令)模块获取当前时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.