1,添加权限:
<uses-permission android:name="android.permission.WAKE_LOCK" />
2,代码如下:
// 唤醒屏幕private static PowerManager mPowerManager;private static PowerManager.WakeLock mWakeLock;@SuppressLint("InvalidWakeLockTag")public static void gotoWakeLock(){//亮屏逻辑代码if (mPowerManager == null) {mPowerManager = (PowerManager) ApplicationBase.getContext().getSystemService(Context.POWER_SERVICE);}if (mWakeLock == null) {mWakeLock = mPowerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "TAG");}boolean screenOn = mPowerManager.isScreenOn();if (!screenOn) {//屏幕会持续点亮mWakeLock.acquire(10*60*1000L /*10 minutes*/);//释放锁,以便10分钟后熄屏mWakeLock.release();}}
需要唤醒的时候调用gotoWalkLock()方法就可以了!
3,但是如果此时锁屏了,就需要调用解锁方法:
首先是增加权限:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
// 屏幕解锁public void gotoDisLock(Context context){ KeyguardManager keyguardManager = (KeyguardManager) ApplicationBase.getContext().getSystemService(Context.KEYGUARD_SERVICE);KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("unLock");// 屏幕锁定keyguardLock.reenableKeyguard();// 屏幕解锁keyguardLock.disableKeyguard();}
调用gotoDisLock()方法就可以了!