gpt4 book ai didi

android - 检查是否启用了锁定

转载 作者:IT老高 更新时间:2023-10-28 21:54:33 24 4
gpt4 key购买 nike

我必须检查设置中是否启用了系统锁定。

我使用了下面的代码

boolean b = android.provider.Settings.System.getInt(
getContentResolver(),Settings.System.LOCK_PATTERN_ENABLED, 0)==1;

如果我设置 pattern 锁定返回 true,如果我设置 pin/password 密码返回 false。

我需要检查是否启用了锁定,或者它是 pattern/pin/password 设置中的锁定。

我的代码只适用于 pattern 锁定而不是 pin/password 锁定。

所以请告诉我如何检查所有类型的锁。

最佳答案

所以这个问题已经很老了,但似乎还没有什么好的答案。经过一些源代码(来自 Ramakrishna's link)研究和 self 实验后,我编写了简单的类来完成这项工作。

public class LockType
{
private final static String PASSWORD_TYPE_KEY = "lockscreen.password_type";

/**
* This constant means that android using some unlock method not described here.
* Possible new methods would be added in the future releases.
*/
public final static int SOMETHING_ELSE = 0;

/**
* Android using "None" or "Slide" unlock method. It seems there is no way to determine which method exactly used.
* In both cases you'll get "PASSWORD_QUALITY_SOMETHING" and "LOCK_PATTERN_ENABLED" == 0.
*/
public final static int NONE_OR_SLIDER = 1;

/**
* Android using "Face Unlock" with "Pattern" as additional unlock method. Android don't allow you to select
* "Face Unlock" without additional unlock method.
*/
public final static int FACE_WITH_PATTERN = 3;

/**
* Android using "Face Unlock" with "PIN" as additional unlock method. Android don't allow you to select
* "Face Unlock" without additional unlock method.
*/
public final static int FACE_WITH_PIN = 4;

/**
* Android using "Face Unlock" with some additional unlock method not described here.
* Possible new methods would be added in the future releases. Values from 5 to 8 reserved for this situation.
*/
public final static int FACE_WITH_SOMETHING_ELSE = 9;

/**
* Android using "Pattern" unlock method.
*/
public final static int PATTERN = 10;

/**
* Android using "PIN" unlock method.
*/
public final static int PIN = 11;

/**
* Android using "Password" unlock method with password containing only letters.
*/
public final static int PASSWORD_ALPHABETIC = 12;

/**
* Android using "Password" unlock method with password containing both letters and numbers.
*/
public final static int PASSWORD_ALPHANUMERIC = 13;

/**
* Returns current unlock method as integer value. You can see all possible values above
* @param contentResolver we need to pass ContentResolver to Settings.Secure.getLong(...) and
* Settings.Secure.getInt(...)
* @return current unlock method as integer value
*/
public static int getCurrent(ContentResolver contentResolver)
{
long mode = android.provider.Settings.Secure.getLong(contentResolver, PASSWORD_TYPE_KEY,
DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
if (mode == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING)
{
if (android.provider.Settings.Secure.getInt(contentResolver, Settings.Secure.LOCK_PATTERN_ENABLED, 0) == 1)
{
return LockType.PATTERN;
}
else return LockType.NONE_OR_SLIDER;
}
else if (mode == DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK)
{
String dataDirPath = Environment.getDataDirectory().getAbsolutePath();
if (nonEmptyFileExists(dataDirPath + "/system/gesture.key"))
{
return LockType.FACE_WITH_PATTERN;
}
else if (nonEmptyFileExists(dataDirPath + "/system/password.key"))
{
return LockType.FACE_WITH_PIN;
}
else return FACE_WITH_SOMETHING_ELSE;
}
else if (mode == DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC)
{
return LockType.PASSWORD_ALPHANUMERIC;
}
else if (mode == DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC)
{
return LockType.PASSWORD_ALPHABETIC;
}
else if (mode == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC)
{
return LockType.PIN;
}
else return LockType.SOMETHING_ELSE;
}

private static boolean nonEmptyFileExists(String filename)
{
File file = new File(filename);
return file.exists() && file.length() > 0;
}
}

现在你只需要做

int lockType = LockType.getCurrent(getContentResolver());

来自您的Activity类(class)。如果你想检查一些锁类型然后使用 switch statement

switch (lockType) 
{
case LockType.FACE_WITH_PATTERN:
case LockType.FACE_WITH_PIN:
case LockType.PATTERN:
/* do something */
break;
}

或者如果您只想要“面部解锁”,无论使用哪种附加方法

if (lockType >= LockType.FACE_WITH_PATTERN && lockType <= LockType.FACE_WITH_SOMETHING_ELSE)
{
/* do something */
}

编辑:所以,我在 3 部手机上测试这个类(class),似乎并非所有手机都能正确检测到面部解锁方法。在某些手机上 PASSWORD_QUALITY_BIOMETRIC_WEAK返回和 PASSWORD_QUALITY_SOMETHING在别人身上。我想我们可以检查一些包含面部解锁信息的文件是否存在且不为空,类似于密码/pin 和模式方法。但是现在我不知道这个文件到底在哪里。

EDIT2:看起来我在 android 4.3 源代码研究后发现了这个问题。那是因为锁定设置已移动到新位置(/data/system/locksettings.db),并且似乎无法从该数据库中获取这些设置(rw-rw----权限和“系统”所有者和组所以只有root才能完成这项工作)。

关于android - 检查是否启用了锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7768879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com