如何更改Android Things设备上的日期?

时间:2016-12-27 09:52:28

标签: android raspberry-pi raspberry-pi3 android-things

我在Raspberry Pi 3 Model B上使用Android Things Developer Preview。有没有办法设置正确的日期/时间/时区?

2 个答案:

答案 0 :(得分:4)

如果您的应用是系统应用,则可以执行以下操作:

在AndroidManifest中:

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

在您的活动中:

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

这会将系统时间设置为2009/9月/ 9日see here for API docs

然而要使用SET_TIME权限,您需要成为system app

不幸的是,当使用 Android Studio中的常规ADB安装时,它不会将您的应用安装为系统应用。您需要使用其他ADB命令手动执行此操作。

这是可能的,因为你应该在任何开发者设备上 root访问权限,这里有一个解释如何将你的apk作为系统应用程序推送,然后你可以设置如上所述的时间!

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

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

如果您没有权限(不是系统应用),您会在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)

答案 1 :(得分:4)

最简单的方法是在ADB上使用ByVal shell命令。它需要root,但所有预览图像都应允许root访问。这是一个检查日期,设置日期,然后验证日期更改卡住的示例:

date

您可以通过$ 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 确定日期命令接受的格式。请注意,由于大多数开发工具包没有电池供电的RTC,因此这种更改可能无法在电源循环中持续存在。

相关问题