Ask for unlock pattern - Android

我的梦境 提交于 2019-12-06 01:06:26

问题


Is there a way, I can ask a user for perform the unlock operation on his phone using the set pattern(passcode, fingerprint, etc) to access certain features of my application?

For example, in iOS, I generate an OTP based on a QR code. I can ask the user for the unlock pin before showing him the generated OTP token. I want the same for my android application. This prevents the misuse of the application.


回答1:


You can create that intent with the KeyguardManager class, using the createConfirmDeviceCredentialIntent method available since API 21, it should be called from your activity with the startActivityForResult(intent) method.

In your activity:

private static final int CREDENTIALS_RESULT = 4342; //just make sure it's unique within your activity.

void checkCredentials() {
  KeyguardManager keyguardManager = this.getSystemService(Context.KEYGUARD_SERVICE);
  Intent credentialsIntent = keyguardManager.createConfirmDeviceCredentialIntent("Password required", "please enter your pattern to receive your token");
  if (credentialsIntent != null) {
    startActivityForResult(credentialsIntent, CREDENTIALS_RESULT);
  } else {
    //no password needed
    doYourThing();
  }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Bundle data) {
  if (requestCode == CREDENTIALS_RESULT) {
    if(resultCode == RESULT_OK) {
      //hoorray!
      doYourThing();
    } else {
      //uh-oh
      showSomeError();
    }
  }
}


来源:https://stackoverflow.com/questions/37629634/ask-for-unlock-pattern-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!