2016-12-27 26 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)