2016-12-27 88 views

回答

3

最简单的方法是使用date在亚行的shell命令。它需要root权限,但所有预览图像都应允许root访问。下面是检查的日期,设置它,然后一个例子验证日期变更卡:

$ 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 

您可以确定date命令的格式通过adb shell date -h接受。请注意,由于大多数开发套件没有电池供电的RTC,因此这种更改可能不会持续通过电源周期。

+0

作品完美,谢谢! –

3

如果您的应用程序是一个系统应用程序,你可以像下面的:

在你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)