Detailed Explanation of Flow Code of Calling Password Lock Screen Verification by Android

  • 2021-12-13 09:47:21
  • OfStack

How to realize Android calling password lock screen verification? Many friends to this step when I do not know how to write the code, the following site I write the core code to share out, the need for friends directly to modify the next can be used!

1: Get manager


KeyguardManager mKeyguardMgr = null;
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mKeyguardMgr = getSystemService(KeyguardManager.class);
        }

2: Create intent


/**
     *  Jump Screen Lock Password Verification Page 
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void showScreenLockPwd() {
        Intent intent = mKeyguardMgr.createConfirmDeviceCredentialIntent(null, null);
        if (intent != null) {
            startActivityForResult(intent, 1101);
        } else {
            Toast.makeText(this, "intent==null", Toast.LENGTH_LONG).show();
        }
    }

3: Listen for callbacks


/**
     * @param requestCode
     * @param resultCode
     * @param data         Screen lock password check callback 
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1101) {
            if (resultCode == RESULT_OK) {
                Toast.makeText(this, " Verification succeeded ", Toast.LENGTH_LONG).show();
            } else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    showScreenLockPwd();
                }
            }
        }
    }

4: Compatibility issues
onactivityresult will be executed twice on Xiaomi 11.
So delay the jump on Xiaomi 11 for 1 second


 // Jump password verification   No pin Code time intent==null
        Intent intent = mKeyguardMgr.createConfirmDeviceCredentialIntent(null, null);
        if (intent != null) {
            if (ClientSdkAppEnv.isMiRom()) {
                // Millet delay 1 Second execution ( The Lock Screen Verification page uses android:taskAffinity) , 
                //  Millet 11 Execute twice on the onactivityresult The situation of 
                Scheduler.dispatchUI(() -> startActivityForResult(intent, ACTIVITY_FOR_RESULT), Dates.MILLIS_PER_SECOND);
            } else {
                startActivityForResult(intent, ACTIVITY_FOR_RESULT);
            }
        } else {
          // Jump password verification   No pin Code time intent==null
          Toast.makeText(this, "intent==null", Toast.LENGTH_LONG).show();
        }

Related articles: