判断设备是否设置支持终端解锁
public class LockUtils {
private static KeyguardManager keyManager;
public static boolean isTerminalLockUse(Context context) {
keyManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return isFingerAndKeyUse(context);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
return keyManager != null && keyManager.isKeyguardSecure();
} else {
return isSecured(context);
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
private static boolean isFingerAndKeyUse(Context context) {
FingerprintManager manager = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
return Objects.requireNonNull(manager).hasEnrolledFingerprints() || keyManager.isKeyguardSecure();
}
private static boolean isSecured(Context context) {
boolean isSecured;
String classPath = "com.android.internal.widget.LockPatternUtils";
try {
@SuppressLint("PrivateApi")
Class<?> lockPatternClass = Class.forName(classPath);
Object lockPatternObject =lockPatternClass.getConstructor(Context.class).newInstance(context);
Method method = lockPatternClass.getMethod("isSecure");
isSecured = (boolean) method.invoke(lockPatternObject);
} catch (Exception e) {
isSecured = false;
}
return isSecured;
}
}