我在 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");
如果网络连接到树莓派 设置时间没问题。
但我的问题只是在启动设备时,没有连接网络。
如果设备未连接网络。基本上时间设置为 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没有内置实时时钟 (RTC),如果没有网络连接或使用外部电池供电的 RTC 模块,如 DS1307,它不可能获得实际当前时间。或 DS3231或许多其他(也请参阅 this 手册)。通常 RTC 模块使用 I2C接口(interface),因此您应该将 RTC 模块连接到您的电路板,最初(当您的电路板连接到网络并且当前时间已知时)通过 I2C 为其设置实际时间,然后在启动时从 RTC 模块获取当前时间并将其设置为Android Things 系统如上例所示。如何通过 I2C 控制 DS3231 RTC 你可以找到 here . User space 内部结构您可以找到该示例的驱动程序 there .
您还可以从 GPS 获取当前时间(例如来自 RMC
句子)和 GSM (例如 AT+CLTS
命令)通过 UART 连接的模块.