问题描述
我要检查系统锁定是否启用与否的设置。
I have to check whether system lock was enabled or not in settings.
我用下面一行code
I used below line code
boolean b = android.provider.Settings.System.getInt(
getContentResolver(),Settings.System.LOCK_PATTERN_ENABLED, 0)==1;
如果设置了模式
锁,它假
这将返回true,如果我设置 PIN /密码
密码。
It returns true if i set pattern
lock and it false for if i set pin/password
password.
我需要检查锁是否启用与否要么是模式/ PIN /密码
锁设置。
I need to check whether lock was enabled or not either it is pattern/pin/password
lock in settings.
我的code仅适用于模式
锁定不要 PIN /密码
锁定
My code is only works to pattern
lock not to pin/password
lock.
所以,请告诉我如何检查所有类型的锁。
So please tell me how to check for all type of locks.
推荐答案
所以这个问题是pretty的旧的,但它似乎也没有一些很好的答案。经过一番源$ C $ C(从罗摩克里希纳的链接)的研究和自我实验,我写的做的工作简单的类
So this question is pretty old but it seems there is no some good answer yet. After some source code (from Ramakrishna's link) research and self experiments I'm wrote simple class that do the job.
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());
from your Activity class.If you want to check some set of lock types then just use switch statement
switch (lockType)
{
case LockType.FACE_WITH_PATTERN:
case LockType.FACE_WITH_PIN:
case LockType.PATTERN:
/* do something */
break;
}
如果你只想要人脸解锁无论与附加的方法
or if you want only "Face Unlock" no matter with which additional method
if (lockType >= LockType.FACE_WITH_PATTERN && lockType <= LockType.FACE_WITH_SOMETHING_ELSE)
{
/* do something */
}
编辑:所以,我测试这个类的3个电话,似乎并不是所有的手机都正确地检测人脸解锁方法。在某些手机上PASSWORD_QUALITY_BIOMETRIC_WEAK回报和PASSWORD_QUALITY_SOMETHING在anothers。我认为我们可以检查一些文件,它包含人脸解锁的信息是存在的,而不是空洞的,类似于密码/ pin和模式的方法。但现在我不知道究竟该文件。
so, I'm test this class on 3 phones and it seems not all phones correctly detect face unlock method. On some phones PASSWORD_QUALITY_BIOMETRIC_WEAK returns and PASSWORD_QUALITY_SOMETHING on anothers. I think we can check some file that contains info for face unlock is exists and not empty, similar to password/pin and pattern methods. But for now I don't know where exactly this file is.
EDIT2:看来我发现后的Android 4.3 SORCE code研究的问题。这是因为锁的设置已移动到新位置(/data/system/locksettings.db),似乎没有办法让那些从这个数据库(RW-RW ----权限和系统的所有者和组这样的设置只有root用户可以做的工作)。
Looks like I found the problem after android 4.3 sorce code research. That's because lock settings was moved to new location (/data/system/locksettings.db) and it seems there is no way to get those setting from this database (rw-rw---- permissions and "system" owner and group so only root can do the job).
这篇关于检查锁是否启用与否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!