ENV: Android M 6.0.1
import android.os.UserHandle;
import com.android.internal.widget.LockPatternUtils;
import android.app.admin.DevicePolicyManager;
LockPatternUtils mLockPatternUtils = new LockPatternUtils(mContext);
int userId = UserHandle.myUserId();
boolean lockScreenDisabled = mLockPatternUtils.isLockScreenDisabled(userId);
boolean security = mLockPatternUtils.isSecure(userId);
boolean isLockPasswordEnabled = mLockPatternUtils.isLockPasswordEnabled(userId);
boolean isLockPatternEnabled = mLockPatternUtils.isLockPatternEnabled(userId);
final int quality = mLockPatternUtils.getKeyguardStoredPasswordQuality(userId);
Log.d(TAG, "lockScreenDisabled = " + lockScreenDisabled);
Log.d(TAG, "security = " + security);
Log.d(TAG, "isLockPasswordEnabled = " + isLockPasswordEnabled);
Log.d(TAG, "isLockPatternEnabled = " + isLockPatternEnabled);
Log.d(TAG, "quality = " + quality);
switch (quality) {
case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
if (mLockPatternUtils.isLockPasswordEnabled(userId)) {
Log.d(TAG, "PIN码 安全模式");
}
break;
case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
if (mLockPatternUtils.isLockPasswordEnabled(userId)) {
Log.d(TAG, "密码 安全模式");
}
break;
case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
case DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED:
if (mLockPatternUtils.isLockPatternEnabled(userId)) {
Log.d(TAG, "图案 安全模式");
}
break;
}
打印Log
[email protected]:~$ adb logcat -s LockPatternUtilsHelp
--------- beginning of system
--------- beginning of main
//图案
11-02 14:36:22.721 4896 4896 D LockPatternUtilsHelp: lockScreenDisabled = false
11-02 14:36:22.721 4896 4896 D LockPatternUtilsHelp: security = true
11-02 14:36:22.721 4896 4896 D LockPatternUtilsHelp: isLockPasswordEnabled = false
11-02 14:36:22.721 4896 4896 D LockPatternUtilsHelp: isLockPatternEnabled = true
11-02 14:36:22.722 4896 4896 D LockPatternUtilsHelp: quality = 65536
11-02 14:36:22.722 4896 4896 D LockPatternUtilsHelp: 图案 安全模式
//PIN码
11-02 14:37:34.748 4896 4896 D LockPatternUtilsHelp: lockScreenDisabled = false
11-02 14:37:34.748 4896 4896 D LockPatternUtilsHelp: security = true
11-02 14:37:34.748 4896 4896 D LockPatternUtilsHelp: isLockPasswordEnabled = true
11-02 14:37:34.748 4896 4896 D LockPatternUtilsHelp: isLockPatternEnabled = false
11-02 14:37:34.748 4896 4896 D LockPatternUtilsHelp: quality = 131072
11-02 14:37:34.748 4896 4896 D LockPatternUtilsHelp: PIN码 安全模式
// 密码
11-02 14:38:10.930 4896 4896 D LockPatternUtilsHelp: lockScreenDisabled = false
11-02 14:38:10.930 4896 4896 D LockPatternUtilsHelp: security = true
11-02 14:38:10.930 4896 4896 D LockPatternUtilsHelp: isLockPasswordEnabled = true
11-02 14:38:10.930 4896 4896 D LockPatternUtilsHelp: isLockPatternEnabled = false
11-02 14:38:10.930 4896 4896 D LockPatternUtilsHelp: quality = 327680
11-02 14:38:10.931 4896 4896 D LockPatternUtilsHelp: 密码 安全模式
// 滑动
11-02 14:36:58.968 4896 4896 D LockPatternUtilsHelp: lockScreenDisabled = false
11-02 14:36:58.968 4896 4896 D LockPatternUtilsHelp: security = false
11-02 14:36:58.968 4896 4896 D LockPatternUtilsHelp: isLockPasswordEnabled = false
11-02 14:36:58.968 4896 4896 D LockPatternUtilsHelp: isLockPatternEnabled = false
11-02 14:36:58.968 4896 4896 D LockPatternUtilsHelp: quality = 0
// 无
11-02 14:36:58.968 4896 4896 D LockPatternUtilsHelp: lockScreenDisabled = true
11-02 14:36:58.968 4896 4896 D LockPatternUtilsHelp: security = false
11-02 14:36:58.968 4896 4896 D LockPatternUtilsHelp: isLockPasswordEnabled = false
11-02 14:36:58.968 4896 4896 D LockPatternUtilsHelp: isLockPatternEnabled = false
11-02 14:36:58.968 4896 4896 D LockPatternUtilsHelp: quality = 0
一 如何判断当前锁定方式是否为"无"?
通过LockPatternUtils类的isLockScreenDisabled判断
二 如何判断当前锁定方式是否为"滑动"?
通过LockPatternUtils类的! isSecure方法和! isLockScreenDisabled方法判断
三 如何判断当前锁定方式是否为"安全密码"锁屏?
通过LockPatternUtils类的isSecure方法和getKeyguardStoredPasswordQuality方法判断
PS:LockPatternUtils类为平台内部类,第三方应用层无法获取该类,可考虑通过反射调用亦可考虑其他方式判断