How to change the date on Android Things device?

2.6k views Asked by At

I'm using Android Things Developer Preview on Raspberry Pi 3 Model B. Is there any way to set the correct date/time/timezone?

2

There are 2 answers

0
devunwired On BEST ANSWER

The easiest way to do this is probably using the date shell command over ADB. It requires root, but all the preview images should allow root access. Here's an example that checks the date, sets it, and then verifies that the date change stuck:

$ adb root
  restarting adbd as root    
$ adb shell date
  Sat Jan  1 00:02:40 GMT 2000
$ adb shell date 1227120016
  Tue Dec 27 12:00:00 GMT 2016
$ adb shell date
  Tue Dec 27 12:00:02 GMT 2016

You can determine the format of the date command accepts via adb shell date -h. Be aware that this change may not persist through a power cycle as most of the dev kits don't have a battery-backed RTC.

0
Blundell On

If your app is a system app you can do it like the below:

In your AndroidManifest:

 <uses-permission android:name="android.permission.SET_TIME" />

in your activity:

    Calendar c = Calendar.getInstance();
    c.set(2009, 9, 9, 12, 0, 0);
    AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    am.setTime(c.getTimeInMillis());

This would set the system time to 2009/September/9th, see here for API docs

However to use the SET_TIME permission you need to be a system app.

Unfortunately when using the normal ADB install from Android Studio, it does not install your app as a system app. You would need to do it manually using other ADB commands.

This is possible as you should have root access on any developer device, there is a walk through here explaining how to push your apk as a system app, and then you can set the time as above!

https://android.stackexchange.com/questions/76976/how-to-install-app-as-system-app

or

https://android.stackexchange.com/questions/27/how-do-i-properly-install-a-system-app-given-its-apk

If you do not have the permission (are not a system app) you will see an error like this in your LogCat:

Caused by: java.lang.SecurityException: setTime: Neither user 10026 nor current process has android.permission.SET_TIME.
 at android.os.Parcel.readException(Parcel.java:1683)
 at android.os.Parcel.readException(Parcel.java:1636)
 at android.app.IAlarmManager$Stub$Proxy.setTime(IAlarmManager.java:224)
 at android.app.AlarmManager.setTime(AlarmManager.java:937)
 at com.blundell.MainActivity.onCreate(MainActivity.java:19)