How to change the date on Android Things device?

前端 未结 2 1057
青春惊慌失措
青春惊慌失措 2021-01-20 16:46

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条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-20 17:20

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

    In your AndroidManifest:

     
    

    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)
    

提交回复
热议问题