Android保持屏幕常亮,PowerManager.WakeLock的使用 需要在AndroidManifest.xml中添加許可權<uses-permission android:name="android.permission.WAKE_LOCK"/> SCREEN_BRIGHT_WAKE_LO ...
Android保持屏幕常亮,PowerManager.WakeLock的使用
package com.hebaijun.wakelock; import android.app.Activity; import android.os.Bundle; import android.os.PowerManager; public class WakeLockActivity extends Activity { PowerManager.WakeLock wakeLock; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); wakeLock = ((PowerManager)getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "WakeLockActivity"); } @Override protected void onPause() { super.onPause(); if (wakeLock != null) { wakeLock.release(); } } @Override protected void onResume() { super.onResume(); wakeLock.acquire(); } }
需要在AndroidManifest.xml中添加許可權<uses-permission android:name="android.permission.WAKE_LOCK"/>
- SCREEN_BRIGHT_WAKE_LOCK表示屏幕亮,鍵盤不亮。
- ON_AFTER_RELEASE表示執行 release方法後繼續保持屏幕亮,直到預設超時後黑屏。
- 最好在 onResume方法中請求常亮,在 onPause方法中釋放。