I'm able to change the password,pin or Pattern from the below code, on Click function of Switch.
switchPreferenceAppLock.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (!((Boolean) newValue)) {
} else {
Log.d("Test", "Change to Phone Default Lock");
Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
startActivity(intent);
return true;
}
});
}
我的目的是在我的应用中使用用户的手机安全系数(如图案,图钉,密码)。就像在我的应用程序用户想要执行任何关键功能一样,然后会出现安全屏幕并要求进行身份验证,并且在身份验证之后,我将允许用户执行该功能。
但它仅用于更改密码,我想将其设置为锁定手机而不是更改。意图DevicePolicyManager.ACTION_SET_NEW_PASSWORD将仅用于更改密码。如何设置应用程序锁定设置我的应用程序的屏幕。
答案 0 :(得分:2)
在API 21+上,您可以使用KeyguardManager.createConfirmDeviceCredentialIntent(...)
创建一个新的Intent
,将用户带到锁定屏幕以确认身份,您可以从startActivityForResult(...)
开始。这是启动锁屏意图的快速示例:
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE)
boolean isSecure = keyguardManager.isKeyguardSecure();
if (isSecure) {
Intent intent = keyguardManager.createConfirmDeviceCredentialIntent(null, null);
startActivityForResult(intent, YOUR_REQUEST_CODE);
} else {
// no lock screen set, show the new lock needed screen
}
然后检查结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case YOUR_REQUEST_CODE:
if (resultCode == RESULT_OK) {
// user authenticated
} else {
// user did not authenticate
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}