Reputation: 77
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;
}
});
}
My purpose is to use the user's phone security factor (like pattern, pin, password) in my app. Like if in my app user wants to do any critical function then security screen will be appeared and will ask to authenticate and after the authentication, i will allow the user to do the functionality.
But It is only used to change the password,I want to set this to lock the phone instead of changing.The Intent DevicePolicyManager.ACTION_SET_NEW_PASSWORD will only be used to change the password.How Can I set the app lock int he same setting screen of my App.
Upvotes: 1
Views: 1156
Reputation: 6393
On API 21+ you can use KeyguardManager.createConfirmDeviceCredentialIntent(...)
to create a new Intent
that will take the user to the lock screen to confirm the identity, which you can start with startActivityForResult(...)
. Here's a quick sample for starting the lock screen intent:
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
}
Then you check the results:
@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);
}
}
Upvotes: 3