2 fsn001797 fsn001797 于 2015.06.19 10:48 提问

安卓唤醒屏幕变亮的方法

执行后,唤醒屏幕变亮的方法?求大牛告知。
例如:本身是黑屏状态,执行了唤醒代码后,屏幕自动变亮。

2个回答

lzp_lrp
lzp_lrp   Ds   Rxr 2015.06.19 11:19

参考以下代码

 解锁、唤醒屏幕用到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();

       }





}


qq_29155621
qq_29155621   2015.06.19 12:40

//锁屏、唤醒相关

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();

   }

}

Csdn user default icon
上传中...
上传图片
插入图片