android 屏幕保持唤醒 不锁屏 android.permission.WAKE_LOCK
2017-11-08 17:20
477 查看
In AndroidManifest.xml 加上权限: <uses-permission android:name="android.permission.WAKE_LOCK" /> 方法一: public class UnLockActivity2 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } } 方法二: public class UnLockActivity extends Activity { /** Called when the activity is first created. */ WakeLock m_wklk; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE); m_wklk = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "cn"); m_wklk.acquire(); //设置保持唤醒 } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); m_wklk.release(); //解除保持唤醒 } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); m_wklk.release();//解除保持唤醒 } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); m_wklk.acquire(); //设置保持唤醒 } } 解释: 用到的类 PowerManager 主要是这两个参数:PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE 下面是 android 官方API 解释: he following flags are defined, with varying effects on system power.These flags are mutually exclusive - you may only specify one of them. Flag Value CPU Screen Keyboard PARTIAL_WAKE_LOCK On* Off Off SCREEN_DIM_WAKE_LOCK On Dim Off SCREEN_BRIGHT_WAKE_LOCK On Bright Off FULL_WAKE_LOCK On Bright Bright 一般要使程序运行过程中背景保持常亮,使用 SCREEN_BRIGHT_WAKE_LOCK 就可以, SCREEN_BRIGHT_WAKE_LOCK CPU:唤醒 屏幕背光:唤醒 键盘灯:关闭 第二个参数: In addition, you can add two more flags, which affect behavior of the screen only.These flags have no effect when combined with aPARTIAL_WAKE_LOCK. Flag Value Description ACQUIRE_CAUSES_WAKEUP Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately. ON_AFTER_RELEASE If this flag is set, the user activity timer will be reset when the WakeLock is released, causing the illumination to remain on a bit longer. This can be used to reduce flicker if you are cycling between wake lock conditions.
相关文章推荐
- android 屏幕保持唤醒 不锁屏 android.permission.WAKE_LOCK
- android 屏幕保持唤醒 不锁屏 android.permission.WAKE_LOCK
- android 屏幕保持唤醒 不锁屏 android.permission.WAKE_LOCK
- android 屏幕保持唤醒 不锁屏 android.permission.WAKE_LOCK
- android 屏幕保持唤醒 不锁屏 android.permission.WAKE_LOCK
- android 屏幕保持唤醒 不锁屏 android.permission.WAKE_LOCK
- Android保持屏幕常亮,PowerManager.WakeLock的使用
- Android异常:唤醒锁未授权。(Caused by: java.lang.SecurityException: Neither user 10044 nor current process has android.permission.WAKE_LOCK.)
- android屏幕常亮保持(WakeLock & WifiLock的实现)
- android屏幕休眠和唤醒两种方法(newWakeLock)
- Android 屏幕保持唤醒
- android 屏幕保持不锁屏
- android锁屏唤醒并解锁屏幕
- android 休眠唤醒机制分析(一) — wake_lock
- Android中休眠与唤醒之wake_lock, early_suspend, late_resume
- android锁屏状态下, 新消息唤醒屏幕,并跳转到指定页面
- android唤醒屏幕/保持屏幕唤醒
- Android在标准linux基础上对休眠唤醒的实现(wakelock)
- android保持服务不休眠(持续运行)以及唤醒屏幕的方法
- 「转」 Android中休眠与唤醒之wake_lock, early_suspend, late_resume