導航:首頁 > 操作系統 > androidsystemclock

androidsystemclock

發布時間:2025-08-20 19:07:37

android 如何在應用程序中修改系統時間

在 android 的API中有提供 SystemClock.setCurrentTimeMillis()函數來修改系統時間,可惜無論你怎麼調用這個函數都是沒用的,無論模擬器還是真機,在logcat中總會得到"Unable to open alarm driver: Permission denied ".這個函數需要root許可權或者運行與系統進程中才可以用。
本來以為就沒有辦法在應用程序這一層改系統時間了,後來在網上搜了好久,知道這個目的還是可以達到的。
第一個方法簡單點,不過需要在Android系統源碼的環境下用make來編譯
1. 在應用程序的AndroidManifest.xml中的manifest節點中加入
"android:sharedUserId="android.uid.system"這個屬性。
2. 修改Android.mk文件,加入LOCAL_CERTIFICATE := platform這一行
3. 使用mm命令來編譯,生成的apk就有修改系統時間的許可權了。

第二個方法麻煩點,不過不用開虛擬機跑到源碼環境下用make來編譯:
1. 同上,加入"android:sharedUserId="android.uid.system"這個屬性。
2. 使用eclipse編譯出apk文件,但是這個apk文件是不能用的。
3. 用壓縮軟體打開apk文件,刪掉META-INF目錄下的CERT.SF和CERT.RSA兩個文件。
4. 使用目標系統的platform密鑰來重新給apk文件簽名。這步比較麻煩,
首先找到密鑰文件,在我的Android源碼目錄中的位置是"build argetproctsecurity",下面的platform.pk8和platform.x509.pem兩個文件。
然後用Android提供的Signapk工具來簽名,signapk的源代碼是在"build oolssignapk"下,用法為"signapk platform.x509.pem platform.pk8 input.apk output.apk",文件名最好使用絕對路徑防止找不到,也可以修改源代碼直接使用。這樣最後得到的apk和第一個方法是一樣的。
最後解釋一下原理,首先加入android:sharedUserId="android.uid.system"這個屬性。通過Shared User id,擁有同一個User id的多個APK可以配置成運行在同一個進程中。那麼把程序的UID配成android.uid.system,也就是要讓程序運行在系統進程中,這樣就有許可權來修改系統時間了。
只是加入UID還不夠,如果這時候安裝APK的話發現無法安裝,提示簽名不符,原因是程序想要運行在系統進程中還要有目標系統的platform key,就是上面第二個方法提到的platform.pk8和platform.x509.pem兩個文件。用這兩個key簽名後apk才真正可以放入系統進程中。第一個方法中加入LOCAL_CERTIFICATE := platform其實就是用這兩個key來簽名。
這也有一個問題,就是這樣生成的程序只有在原始的Android系統或者是自己編譯的系統中才可以用,因為這樣的系統才可以拿到 platform.pk8和platform.x509.pem兩個文件。要是別家公司做的Android上連安裝都安裝不了。試試原始的Android 中的key來簽名,程序在模擬器上運行OK,不過放到G3上安裝直接提示"Package ... has no signatures that match those in shared user android.uid.system",這樣也是保護了系統的安全。

❷ SystemClock.sleep和Thread.sleep的區別

在Java中我們處理線程同步問題時,處理延遲可能會使用Thread類的sleep方法,這里拋開concurrent類的一些方法,其實 Android平台還提供了一個SystemClock.sleep方法,它們有什麼區別呢?

我們每次調用Thread.sleep時可能會出現InterruptedException異常,而SystemClock.sleep方法則不會,在 SDK上有這樣的描述,它將會忽略中斷異常。
Three different clocks are available, and they should not be confused:
System.currentTimeMillis() is the standard "wall" clock (time and date) expressing milliseconds since the epoch. The wall clock can be set by the user or the phone network (see setCurrentTimeMillis(long)), so the time may jump backwards or forwards unpredictably. This clock should only be used when correspondence with real-world dates and times is important, such as in a calendar or alarm clock application. Interval or elapsed time measurements should use a different clock. If you are using System.currentTimeMillis(), consider listening to the ACTION_TIME_TICK, ACTION_TIME_CHANGED and ACTION_TIMEZONE_CHANGED Intent broadcasts to find out when the time changes.
uptimeMillis() is counted in milliseconds since the system was booted. This clock stops when the system enters deep sleep (CPU off, display dark, device waiting for external input), but is not affected by clock scaling, idle, or other power saving mechanisms. This is the basis for most interval timing such as Thread.sleep(millls), Object.wait(millis), and System.nanoTime(). This clock is guaranteed to be monotonic, and is the recommended basis for the general purpose interval timing of user interface events, performance measurements, and anything else that does not need to measure elapsed time ring device sleep. Most methods that accept a timestamp value expect the uptimeMillis() clock.
elapsedRealtime() is counted in milliseconds since the system was booted, including deep sleep. This clock should be used when measuring time intervals that may span periods of system sleep.
There are several mechanisms for controlling the timing of events:
Standard functions like Thread.sleep(millis) and Object.wait(millis) are always available. These functions use the uptimeMillis() clock; if the device enters sleep, the remainder of the time will be postponed until the device wakes up. These synchronous functions may be interrupted with Thread.interrupt(), and you must handle InterruptedException.
SystemClock.sleep(millis) is a utility function very similar to Thread.sleep(millis), but it ignores InterruptedException. Use this function for delays if you do not use Thread.interrupt(), as it will preserve the interrupted state of the thread.

❸ android中有什麼和GetTickCount類似的的函數嗎

android api中SystemClock.uptimeMillis()是和gettickcount功能類似的函數:
1.GetTickCount是函數。GetTickCount返回(retrieve)從操作系統啟動所經過(elapsed)的毫秒數,它的返回值是DWORD。
2.SystemClock.uptimeMillis()也是返回程序運行消耗的時間。

閱讀全文

與androidsystemclock相關的資料

熱點內容
江蘇伺服器應用線束加工雲主機 瀏覽:109
安卓手機名字如何變成透明的 瀏覽:116
1進制演算法公式 瀏覽:611
程序員多久敲壞一把機械鍵盤 瀏覽:589
安慶計算機編程培訓班 瀏覽:463
Linux伺服器請求log如何查看 瀏覽:465
outpdf 瀏覽:163
加密錢包市值排名 瀏覽:170
文件怎麼電腦解壓 瀏覽:680
網站源碼傳輸到阿里 瀏覽:293
壓縮照片後解壓會影響清晰度嗎 瀏覽:648
pdf讀寫器 瀏覽:145
java獲取毫秒值 瀏覽:249
r620伺服器如何設置ipm 瀏覽:795
三維轉pdf 瀏覽:195
m1晶元支持python第三方庫嗎 瀏覽:795
從xsd生成java類 瀏覽:438
電腦命令符里如何換行 瀏覽:568
安卓眼睛標志怎麼取消 瀏覽:637
51單片機可以外接mp3嗎 瀏覽:544