Last active
November 5, 2021 10:28
-
-
Save Forgo7ten/78cf5fb113b2ba087e478b276de3b94e to your computer and use it in GitHub Desktop.
Android代码:唤醒手机屏幕并解锁
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void wakeUpAndUnlock(Context context){ | |
// 屏锁管理器 | |
KeyguardManager km= (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); | |
KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock"); | |
// 解锁 | |
kl.disableKeyguard(); | |
// 获取电源管理器对象 | |
PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE); | |
// 获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag | |
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | | |
PowerManager.SCREEN_DIM_WAKE_LOCK,"bright"); | |
// 点亮屏幕 | |
wl.acquire(); | |
// 释放 | |
wl.release(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 调用示例 | |
// 唤醒并解锁 | |
wakeUpAndUnlock(getApplicationContext()); | |
// 启动Activity | |
Intent intent = new Intent(getApplicationContext(),MainActivity.class); | |
startActivity(intent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.WAKE_LOCK" /> | |
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment