欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android唤醒、解锁屏幕代码实例

程序员文章站 2022-07-03 18:56:19
解锁、唤醒屏幕用到keyguardmanager,keyguardlock,powermanager,powermanager.wakelock   所需权限...

解锁、唤醒屏幕用到keyguardmanager,keyguardlock,powermanager,powermanager.wakelock
 
所需权限:

复制代码 代码如下:

<uses-permission android:name="android.permission.wake_lock" />

<uses-permission android:name="android.permission.disable_keyguard" />
 

 

复制代码 代码如下:

//锁屏、唤醒相关
private keyguardmanager  km;
private keyguardlock kl;
private powermanager pm;
private powermanager.wakelock wl;
 
 
 
private void wakeandunlock(boolean b)
{
       if(b)
       {
              //获取电源管理器对象
              pm=(powermanager) getsystemservice(context.power_service);
 
              //获取powermanager.wakelock对象,后面的参数|表示同时传入两个值,最后的是调试用的tag
              wl = pm.newwakelock(powermanager.acquire_causes_wakeup | powermanager.screen_bright_wake_lock, "bright");
 
              //点亮屏幕
              wl.acquire();
            
              //得到键盘锁管理器对象
              km= (keyguardmanager)getsystemservice(context.keyguard_service);
              kl = km.newkeyguardlock("unlock");
 
              //解锁
              kl.disablekeyguard();
       }
       else
       {
              //锁屏
              kl.reenablekeyguard();
            
              //释放wakelock,关灯
              wl.release();
       }
     
}