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;
}
/**
*跳转到系统解锁页面
*/privatevoidshowAuthenticationScreen() {
// 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);
}
}
/**
*接收系统解锁是否成功
*/@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {
// Challenge completed, proceed with using cipherif (resultCode == RESULT_OK) {
if (tryEncrypt()) {
showPurchaseConfirmation();
}
} else {
// The user canceled or didn’t complete the lock screen// operation. Go to error/cancellation flow.
}
}
}