一開始接觸android應(yīng)用項(xiàng)目時,在項(xiàng)目快上線時策劃要求加個高亮,每次都是直接從網(wǎng)上百度一下,現(xiàn)在做游戲了,卻還是這個需求,這里備份一下方便后續(xù)的查找,畢竟是自己寫好且驗(yàn)證沒問題的 :)
首先,在Activity里添加一下代碼:
成員變量:
private PowerManager.WakeLock wakeLock = null; 重寫父類Activity的兩個方法并添加一下邏輯:@Override public void onResume() { super.onResume(); acquireWakeLock(this); } @Override public void onPause() { super.onPause(); releaseWakeLock(); } public void acquireWakeLock(Context context) { if (wakeLock == null) { PowerManager powerManager = (PowerManager) (context .getSystemService(Context.POWER_SERVICE)); wakeLock = powerManager.newWakeLock( PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag"); wakeLock.acquire(); } } public void releaseWakeLock() { if (wakeLock != null && wakeLock.isHeld()) { wakeLock.release(); wakeLock = null; } }
然后,在AndroidManifest.xml里添加如下權(quán)限即可:
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
新聞熱點(diǎn)
疑難解答
圖片精選