本文介绍了Android检查是否设置了锁屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查锁屏是否确实具有Pin或更安全的东西(密码,指纹等).我无法检查是否有密码,密码或图案.

i need to check if the lockscreen does have a Pin or something more secure (Password, Fingerprint etc.). Im able to check if there is a Pin, Password or a Pattern.

KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    return keyguardManager.isKeyguardSecure();

我的问题是我无法检测到锁屏是图案"还是更低的图案.我试过了:

My Problem is that i cant detect if the lockscreen is a Pattern or something lower. I tried this:

        int lockPatternEnable = Settings.Secure.getInt(cr, Settings.Secure.LOCK_PATTERN_ENABLED);

,但已弃用它,并向我抛出错误.我也尝试过这个:

but its deprecated and throws me an error. I also tried this:

            long mode2 = Settings.Secure.getLong(contentResolver, "lockscreen.password_type");

,但这也以SecurityException结尾.

but this ends with an SecurityException too.

有什么方法可以检测锁屏是否有大头针(或更高),或者是否有锁定模式或更低的密码? KeyguardManager不适用于我:/

Is there any way to detect if the lockscreen does have a pin (or higher) or it does a lock pattern or something lower? The KeyguardManager is not useful for me in that way :/

感谢您的帮助!谢谢!

Any Help is appreciated!Thanks!

/编辑

第一个错误是:

               Caused by: java.lang.SecurityException: Settings.Secure.lock_pattern_autolock is deprecated and no longer accessible. See API documentation for potential replacements.

第二个异常是:W/System.err:android.provider.Settings $ SettingNotFoundException:lockscreen.password_type

The Exception for the second one is: W/System.err: android.provider.Settings$SettingNotFoundException: lockscreen.password_type

当您使用带有棉花糖或更高版本的设备( https://developer.android.com/reference/android/provider/Settings.Secure.html )

The Error just appears when youre using devices with Marshmallow or later (https://developer.android.com/reference/android/provider/Settings.Secure.html)

推荐答案

我正在这样做:(灵感来自 https://gist.github.com/doridori/54c32c66ef4f4e34300f )

I'm doing this :(inspired from https://gist.github.com/doridori/54c32c66ef4f4e34300f)

public boolean isDeviceScreenLocked() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return isDeviceLocked();
    } else {
        return isPatternSet() || isPassOrPinSet();
    }
}

/**
 * @return true if pattern set, false if not (or if an issue when checking)
 */
private boolean isPatternSet() {
    ContentResolver cr = context.getContentResolver();
    try {
        int lockPatternEnable = Settings.Secure.getInt(cr, Settings.Secure.LOCK_PATTERN_ENABLED);
        return lockPatternEnable == 1;
    } catch (Settings.SettingNotFoundException e) {
        return false;
    }
}

/**
 * @return true if pass or pin set
 */
private boolean isPassOrPinSet() {
    KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); //api 16+
    return keyguardManager.isKeyguardSecure();
}

/**
 * @return true if pass or pin or pattern locks screen
 */
@TargetApi(23)
private boolean isDeviceLocked() {
    KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); //api 23+
    return keyguardManager.isDeviceSecure();
}

这篇关于Android检查是否设置了锁屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 14:19