有些场景需要程序自动点亮屏幕,解开屏幕锁,以方便用户即时操作,下面用代码来实现这一功能:

        
  1. 1.//得到键盘锁管理器对象
  2. 2.KeyguardManagerkm=(KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
  3. 3.//参数是LogCat里用的Tag
  4. 4.KeyguardLockkl=km.newKeyguardLock("unLock");
  5. 5.//解锁
  6. 6.kl.disableKeyguard();
  7. 7.
  8. 8.//获取电源管理器对象
  9. 9.PowerManagerpm=(PowerManager)getSystemService(Context.POWER_SERVICE);
  10. 10.//获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
  11. 11.PowerManager.WakeLockwl=pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.SCREEN_DIM_WAKE_LOCK,"bright");
  12. 12.//点亮屏幕
  13. 13.wl.acquire();
  14. 14.//释放
  15. 15.wl.release();

需要在AndroidManifest.xml添加权限:

        
  1. <uses-permissionandroid:name="android.permission.WAKE_LOCK"/>
  2. <uses-permissionandroid:name="android.permission.DISABLE_KEYGUARD"/>

flags参数说明:

        
  1. PARTIAL_WAKE_LOCK: Screen off, keyboard light off
  2. SCREEN_DIM_WAKE_LOCK: screen dim, keyboard light off
  3. SCREEN_BRIGHT_WAKE_LOCK: screen bright, keyboard light off
  4. FULL_WAKE_LOCK: screen bright, keyboard bright

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:f 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.

2、保持屏幕唤醒
use the window flag FLAG_KEEP_SCREEN_ON把下面的代码加入到程序onCreate方法中

        
  1. @Override
  2. protectedvoidonCreate(Bundleicicle){
  3. super.onCreate(icicle);
  4. //Setkeepscreenon
  5. getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  6. }

更多相关文章

  1. Android中如何使用Intent传递对象
  2. Retrofit2 ,Dagger2等常用框架注解功能介绍
  3. Android个人使用adb命令总结(持续更新...)
  4. android 纯代码 详细编写布局文件
  5. android绑定点击事件的四种方法
  6. android ueventd 本地native部分源码分析
  7. Android中关于数据库SQLite的insert插入操作的理解
  8. Android(安卓)中文 API (17) —— TextSwitcher
  9. android 快速开发(二)辅助类的使用:加快开发速度

随机推荐

  1. android ndk native_activity.h
  2. Android实现CoverFlow效果
  3. Android(安卓)v2.0 基本概念 - 浅谈
  4. android 手机定位
  5. android5 sdk 下载地址大全
  6. Android(安卓)长按识别图中二维码
  7. “android studio 运行程序提示Applicati
  8. android中textview设置为多行文本时,如何
  9. Android(安卓)Initialization Process --
  10. Android(安卓)Wear Preview- 创建通知(Cre