最近在做App开发时候,需要增加安全性,就采用了添加系统的解锁方案的方式。具体的实现方式如下:
final int REQUSET_CODE_CUSTOM = 0x9900;
KeyguardManager manager = (KeyguardManager)getActivity().getSystemService(Context.KEYGUARD_SERVICE);
if(manager.isKeyguardSecure()){ //判断系统是否设置了相关解锁方式
Intent intent = manager.createConfirmDeviceCredentialIntent(null, null);
if (intent !=null) {
startActivityForResult(intent, REQUEST_CODE_CUSTOM);
}
}
此时如果是Activity则直接重写onActivityResult 方法来处理后面的逻辑即可
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_CUSTOM) {
if (resultCode == RESULT_OK) {
//验证通过逻辑
} else {
// The user canceled or didn’t complete the lock screen
// operation. Go to error/cancellation flow.
}
}
}
如果是Fragment 那么是无法重写OnActivityResult的,需要在Fragment的宿主Activity中先重写onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
getSupportFragmentManager().getFragments();
if (getSupportFragmentManager().getFragments().size() > 0) {
List
for (Fragment mFragment : fragments) {
mFragment.onActivityResult(requestCode, resultCode, data);
}
}
}
然后再在fragment中重写就可以了
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE_CUSTOM:
//验证通过逻辑
break;
}
}
}