代码之家  ›  专栏  ›  技术社区  ›  hyunwookcho

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

  •  1
  • hyunwookcho  · 技术社区  · 7 年前

    我用 Android-Things 启动设备时。如果连接到网络,

    TimeManager 应用程序编程接口。

    private void setupTimeZone(String timeZoneName) {
        TimeManager timeManager = TimeManager.getInstance();
        timeManager.setTimeFormat(TimeManager.FORMAT_24);
        timeManager.setTimeZone(timeZoneName);
    }
    
    setupTimeZone("Asia/Seoul");
    

    如果网络连接到树莓pi 设定时间没问题。

    但我的问题只是在启动设备时,而不是连接网络。

    Jan 1, 2009 09:00

    要更改默认日期,需要修改哪些文件?

    1 回复  |  直到 7 年前
        1
  •  3
  •   Andrii Omelchenko    7 年前

    为您设置可以使用的时间 TimeManager.setTime() 方法:

    要使用时间管理器控制设备设置,请首先请求 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);
    

    但是 Raspberry Pi 3 DS1307 DS3231 或者其他很多人(也可以看看 this I2C 接口,所以您应该将RTC模块连接到您的板上,最初(当您的板连接到网络并且当前时间已知时)通过I2C设置实际时间,然后,在引导时从RTC模块获取当前时间并将其设置为Android Things系统,如上例所示。如何通过I2C控制DS3231 RTC here . 内部 User space there .

    GPS (例如,来自 RMC 句子)和 GSM (例如。 AT+CLTS UART .

    推荐文章