时间:2023-03-17 13:21:52 | 栏目:Android代码 | 点击:次
Android调用密码锁屏校验怎么实现呢?很多朋友到这一步的时候不知如何写代码了,下面小编把我写的核心代码分享出来,有需要的朋友直接拿去修改下即可使用!
1:获取manager
KeyguardManager mKeyguardMgr = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { mKeyguardMgr = getSystemService(KeyguardManager.class); }
2:创建intent
/** * 跳转锁屏密码校验页面 */ @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:监听回调
/** * @param requestCode * @param resultCode * @param data 锁屏密码校验回调 */ @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, "校验成功", Toast.LENGTH_LONG).show(); } else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { showScreenLockPwd(); } } } }
4:兼容性问题
小米11上会出现执行两次onactivityresult的情况。
所以在小米11上面延迟一秒执行跳转
//跳转密码校验 没pin码时intent==null Intent intent = mKeyguardMgr.createConfirmDeviceCredentialIntent(null, null); if (intent != null) { if (ClientSdkAppEnv.isMiRom()) { //小米延迟一秒执行(锁屏验证页面使用android:taskAffinity), // 小米11上会出现执行两次onactivityresult的情况 Scheduler.dispatchUI(() -> startActivityForResult(intent, ACTIVITY_FOR_RESULT), Dates.MILLIS_PER_SECOND); } else { startActivityForResult(intent, ACTIVITY_FOR_RESULT); } } else { //跳转密码校验 没pin码时intent==null Toast.makeText(this, "intent==null", Toast.LENGTH_LONG).show(); }