简体   繁体   English

要求解锁模式 - Android

[英]Ask for unlock pattern - Android

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. 例如,在iOS中,我基于QR码生成OTP。 I can ask the user for the unlock pin before showing him the generated OTP token. 在向他显示生成的OTP令牌之前,我可以询问用户解锁引脚。 I want the same for my android application. 我希望我的Android应用程序一样。 This prevents the misuse of the application. 这可以防止滥用应用程序。

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. 您可以使用KeyguardManager类创建该意图,使用自API 21以来可用的createConfirmDeviceCredentialIntent方法,它应该使用startActivityForResult(intent)方法从您的活动中调用。

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();
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM