当按下电源按钮时,我是否有可能阻止屏幕被锁定。
答案 0 :(得分:2)
你无法停止屏幕锁定(短按),但你可以获得长按键的电源按钮事件
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
//dostuff
return true;
}
return super.dispatchKeyEvent(event);
}
答案 1 :(得分:0)
答案 2 :(得分:-1)
根据您的要求尝试此操作,
KeyguardManager keyguardManager;
KeyguardLock lock;
public void Disable_AutoLock()
{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
}
public void Enable_AutoLock()
{
lock.reenableKeyguard();
}
在Android清单中授予权限。
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>