Android应用程序调用系统解锁页面

Android应用程序调用系统解锁页面

mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);

/**
 *判断设备有没有设置解锁密码
 */
if (!mKeyguardManager.isKeyguardSecure()) {
    // Show a message that the user hasn't set up a lock screen.
    Toast.makeText(this,"Secure lock screen hasn't set up.\n"
           + "Go to 'Settings -> Security -> Screenlock' to set up a lock screen",
            Toast.LENGTH_LONG).show();
    purchaseButton.setEnabled(false);
    return;
} 

/**
 *跳转到系统解锁页面
 */

private void showAuthenticationScreen() {
    // Create the Confirm Credentials screen. You can customize the title and description. Or
    // we will provide a generic one for you if you leave it null
    Intent intent = mKeyguardManager.createConfirmDeviceCredentialIntent(null, null);
    if (intent != null) {
        startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
    }
}

 /**
  *接收系统解锁是否成功
  */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {
        // Challenge completed, proceed with using cipher
        if (resultCode == RESULT_OK) {
            if (tryEncrypt()) {
                showPurchaseConfirmation();
            }
        } else {
            // The user canceled or didn’t complete the lock screen
            // operation. Go to error/cancellation flow.
        }
    }
} 

注意事项

  • 跳转系统解锁页面只能在5.0以上的设备使用,5.0以下的不支持
  • 如果不对设备进行设置密码判断,intent就会返回一个null, 一定要对null值进行判断

参考文档

日本的博客链接
谷歌的官网demo

你可能感兴趣的:(Android)