2

我正面临这里提到的问题Screen Blackout。我无法找到解决方案,让我知道是否有办法解决它。

4

2 回答 2

0

今天我也遇到了这个问题,我找到了一个 kida hacky 解决方案。

关键是重新启用键盘保护,关闭它并再次重新启用它。

private KeyguardManager.KeyguardLock mLock;
public static final String KEYLOCK_NAME = "key_lock";

@Override
protected void onPause() {
    super.onPause();
    // Call it anywhere you need, onPause is just an example
    if (enableKeyguard()) {
        disableKeyguard();
        enableKeyguard();
    }
}
/**
 * Dismisses lockscreen
 */
public void disableKeyguard() {
    if (mLock == null) {
        KeyguardManager manager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
        mLock = manager.newKeyguardLock(KEYLOCK_NAME);
        mLock.disableKeyguard();
    }
}

/**
 * Re-enables lockscreen
 * @return true if lockscreen was previously disabled and now it is enabled again, otherwise false
 */
public boolean enableKeyguard() {
    if (mLock != null) {
        mLock.reenableKeyguard();
        mLock = null;
        return true;
    }

    return false;
}
于 2015-09-23T13:10:23.417 回答
0

您可以使用以下代码:

private BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        SimpleLog.i(TAG, intent.getAction());
        if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)
                || intent.getAction().equals(Intent.ACTION_SCREEN_ON)){

            keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
            keyguardLock = keyguardManager.newKeyguardLock("");
            keyguardLock.disableKeyguard();

            startActivity(lockIntent);

            // **This line is important!!!**
            keyguardLock.reenableKeyguard(); 
        }
    }
};
于 2016-03-16T07:31:32.620 回答