简体   繁体   English

当启动设备时,如果无法连接到网络,如何在android上设置当前时间?

[英]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),没有网络连接或使用DS1307DS3231等外部电池供电的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.

相关问题 如何在 Android Things 上设置设备所有者? - How to set device owner on Android Things? 如何查看 android 设备上的时间是网络设置的还是用户设置的? - How do I check if the time on android device is set by network or by user? 如何以编程方式设置计时器以在特定时间启动Android设备? - How to set a timer programmatically to boot an Android device at a specific time? 获取当前的android设备时间(如果已手动设置) - Get current android device time, if it was set manually 如何通过Android设备连接专用网络 - How to connect private network through android device 如何将android设备与本地网络连接 - How to connect android device with local network 如何在数据库中为不同的语言环境设置当前时间?即使在android移动设备中未设置时间 - how to put current time in database for different locale?, even if in android mobile device time is not set in android 如何获取Android设备的首次启动时间? - How to get the first boot time of an android device? Appium根据当前设备时间设置Android时间 - Appium set Android time based on current device time 设备离线时如何获取 android 设备的正确当前日期和时间 - How can I get correct current date and time for android device while device is offline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM