跳过屏幕锁定

时间:2011-09-23 06:13:03

标签: java android android-widget android-manifest

protected void onPause()
    {
        super.onPause();        

        // If the screen is off then the device has been locked
        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        boolean isScreenOn = powerManager.isScreenOn();

        //screen locked 
        if (!isScreenOn) {

            boolean pressed = onKeyDown(26, null);

            //power button pressed
            if(pressed){
                //remove keyguard
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);                 
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

                //start intent
                Intent i = new Intent(this, VoiceRecognitionActivity.class);
                startActivity(i);
            }


        }
    }    

上面的代码是按下电源按钮时,键盘保护将被解除,并且活动暂停将恢复。

然而,当我按下电源按钮时,键盘保护不会被眩晕,我必须手动解锁。

当我按下电源按钮时,我的活动窗口闪烁了一秒钟,并显示了键盘锁定窗口。

2 个答案:

答案 0 :(得分:0)

如果您想阻止手机关闭屏幕(并锁定手机的结果),您应该使用WakeLock。您可以PowerManager.newWakeLock()使用FLAG_KEEP_SCREEN_ON甚至是FULL_WAKE_LOCK {{1}}。

答案 1 :(得分:0)

此代码段可能有所帮助:

    final Window win = getWindow();
    win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    // Turn on the screen unless we are being launched from the AlarmAlert
    // subclass.
    final boolean screenOff = getIntent().getBooleanExtra(SCREEN_OFF, false);
    if (!screenOff) {
        try {
            // API 8+
            win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
        } catch (final Throwable whocares) {
            // API 7+
            win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        }
    }