Android指纹解锁方法解析
我先说说这两种的方式的不同之处吧
第一种:
在调动成功之后不会让你指纹解锁而是调转到当初你设置指纹解锁时的手势解锁页面
第二种:
在调动成功之后,是进行指纹解锁不调转你直接把手指放到金属感应环上面进行指纹验证
大家可以根据需求自行选择
ok那就亮代码了
第一种:
xml布局:一个文本显示一个按钮(不解释)
MainActivity.java源码
publicclassMainActivityextendsFragmentActivity{
FingerprintManagermanager;
KeyguardManagermKeyManager;
privatefinalstaticintREQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS=0;
privatefinalstaticStringTAG=“finger_log”;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1:先获取指纹识别的的管理者看看获取方式是不是感觉就和数学公式一样
(xxx管理者)=this.getSystemService(Context.xxx服务)
manager=(FingerprintManager)this.getSystemService(Context.FINGERPRINT_SERVICE);
mKeyManager=(KeyguardManager)this.getSystemService(Context.KEYGUARD_SERVICE);
//2初始化按钮设置监听
//监听监听听什么肯定是听听这个手机有没有指纹识别这个功能所以看看isFinger()
Buttonbtn_finger=(Button)findViewById(R.id.btn_activity_main_finger);
btn_finger.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
if(isFinger()){
Toast.makeText(MainActivity.this,"请进行指纹识别",Toast.LENGTH_LONG).show();
Log(TAG,"keyi");
startListening(null);
}
}
});
}
publicbooleanisFinger(){
//androidstudio上,没有这个会报错
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.USE_FINGERPRINT)!=PackageManager.PERMISSION_GRANTED){
Toast.makeText(this,"没有指纹识别权限",Toast.LENGTH_SHORT).show();
returnfalse;
}
Log(TAG,"有指纹权限");
//判断硬件是否支持指纹识别
if(!manager.isHardwareDetected()){
Toast.makeText(this,"没有指纹识别模块",Toast.LENGTH_SHORT).show();
returnfalse;
}
Log(TAG,"有指纹模块");
//判断是否开启锁屏密码
if(!mKeyManager.isKeyguardSecure()){
Toast.makeText(this,"没有开启锁屏密码",Toast.LENGTH_SHORT).show();
returnfalse;
}
Log(TAG,"已开启锁屏密码");
//判断是否有指纹录入
if(!manager.hasEnrolledFingerprints()){
Toast.makeText(this,"没有录入指纹",Toast.LENGTH_SHORT).show();
returnfalse;
}
Log(TAG,"已录入指纹");
returntrue;
}
CancellationSignalmCancellationSignal=newCancellationSignal();
//回调方法
FingerprintManager.AuthenticationCallbackmSelfCancelled=newFingerprintManager.AuthenticationCallback(){
@Override
publicvoidonAuthenticationError(interrorCode,CharSequenceerrString){
//但多次指纹密码验证错误后,进入此方法;并且,不能短时间内调用指纹验证
Toast.makeText(MainActivity.this,errString,Toast.LENGTH_SHORT).show();
showAuthenticationScreen();
}
@Override
publicvoidonAuthenticationHelp(inthelpCode,CharSequencehelpString){
Toast.makeText(MainActivity.this,helpString,Toast.LENGTH_SHORT).show();
}
@Override
publicvoidonAuthenticationSucceeded(FingerprintManager.AuthenticationResultresult){
Toast.makeText(MainActivity.this,"指纹识别成功",Toast.LENGTH_SHORT).show();
}
@Override
publicvoidonAuthenticationFailed(){
Toast.makeText(MainActivity.this,"指纹识别失败",Toast.LENGTH_SHORT).show();
}
};
publicvoidstartListening(FingerprintManager.CryptoObjectcryptoObject){
//androidstudio上,没有这个会报错
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.USE_FINGERPRINT)!=PackageManager.PERMISSION_GRANTED){
Toast.makeText(this,"没有指纹识别权限",Toast.LENGTH_SHORT).show();
return;
}
manager.authenticate(cryptoObject,mCancellationSignal,0,mSelfCancelled,null);
}
/**
*锁屏密码
*/
privatevoidshowAuthenticationScreen(){
Intentintent=mKeyManager.createConfirmDeviceCredentialIntent("finger","测试指纹识别");
if(intent!=null){
startActivityForResult(intent,REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
}
}
@Override
protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
if(requestCode==REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS){
//Challengecompleted,proceedwithusingcipher
if(resultCode==RESULT_OK){
Toast.makeText(this,"识别成功",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this,"识别失败",Toast.LENGTH_SHORT).show();
}
}
}
privatevoidLog(Stringtag,Stringmsg){
Log.d(tag,msg);
}
}
===================================
第二种:
1:xml:布局一个文本提示两个按钮(1:开始识别2:取消识别)
2:咱们将业务操作放到一个FingerprintController类里面
privatestaticFingerprintControllersSingleton=null;
privateContextmContext;
privateFingerprintManagerCompatmanager;
privatestaticfinalStringTAG="FingerprintController";
privatestaticfinalStringPREMISSION="android.permission.USE_FINGERPRINT";
//成功
publicstaticfinalintFINGER_SUCCESS=0;
//硬件不支持
publicstaticfinalintFINGER_ERROR_NO_HARDWARE=1;
//没有申请权限
publicstaticfinalintFINGER_ERROR_NO_PERMISSION=2;
//用户没有赋予权限
//Protectionlevel:normal
//指纹权限的级别是normal,理论上不需要动态权限认证
publicstaticfinalintFINGER_ERROR_NO_USER_PERMISSION=3;
//用户没有储存指纹
publicstaticfinalintFINGER_ERROR_NO_FINGER=4;
//取消指纹识别
privateCancellationSignalcancellationSignal;
privateFingerAuthListenermAuthListener;
publicstaticsynchronizedFingerprintControllergetInstance(Contextcontext){
if(sSingleton==null){
sSingleton=newFingerprintController(context);
}
returnsSingleton;
}
publicFingerprintController(Contextcontext){
mContext=context;
manager=FingerprintManagerCompat.from(this.mContext);
}
/**
*开始指纹识别
*失败次数过多后需要一定时间后才可以重新启动
*/
publicvoidstartFingerAuth(){
if(null==cancellationSignal){
cancellationSignal=newCancellationSignal();
}
manager.authenticate(null,0,cancellationSignal,newFingerAuthCallBack(),null);
}
/**
*取消指纹识别
*/
publicvoidcancelFingerAuth(){
if(cancellationSignal!=null){
cancellationSignal.cancel();
if(mAuthListener!=null)
mAuthListener.cancel();
}
}
/**
*指纹识别回调
*/
publicclassFingerAuthCallBackextendsFingerprintManagerCompat.AuthenticationCallback{
//当出现错误的时候回调此函数,比如多次尝试都失败了的时候,errString是错误信息
@Override
publicvoidonAuthenticationError(interrMsgId,CharSequenceerrString){
//Log.d(TAG,“onAuthenticationError:”+errString);
if(null!=mAuthListener)
mAuthListener.error(errString.toString());
}
//当指纹验证失败的时候会回调此函数,失败之后允许多次尝试,失败次数过多会停止响应一段时间然后再停止sensor的工作
@Override
publicvoidonAuthenticationFailed(){
if(null!=mAuthListener)
mAuthListener.failure();
}
@Override
publicvoidonAuthenticationHelp(inthelpMsgId,CharSequencehelpString){
if(null!=mAuthListener)
Log.e("helpMsgId",helpMsgId+"");
Log.e("helpString",helpString.toString());
mAuthListener.help(helpString.toString());
}
//当验证的指纹成功时会回调此函数,然后不再监听指纹sensor
@Override
publicvoidonAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResultresult){
if(null!=mAuthListener)
mAuthListener.success();
}
}
/**
*检查指纹解锁是否可用
*
*@return状态
*/
publicintcheckFingerEnable(){
if(null==manager){
manager=FingerprintManagerCompat.from(mContext);
}
if(!isAppPermissionEnable()){
returnFINGER_ERROR_NO_PERMISSION;
}
if(!manager.isHardwareDetected()){
returnFINGER_ERROR_NO_HARDWARE;
}
if(!manager.hasEnrolledFingerprints()){
returnFINGER_ERROR_NO_FINGER;
}
if(!isUserPermissionEnable()){
returnFINGER_ERROR_NO_USER_PERMISSION;
}
returnFINGER_SUCCESS;
}
/**
*是否声明了该权限
*/
privatebooleanisAppPermissionEnable(){
PackageManagerpm=mContext.getPackageManager();
if(pm==null){
Log.w(TAG,"can'tgetpackagemanager");
returntrue;
}
try{
returnPackageManager.PERMISSION_GRANTED==pm.checkPermission(PREMISSION,mContext.getPackageName());
}catch(Exceptione){
Log.w(TAG,"can'tchecktPermission"+e.getMessage());
returntrue;
}
}
/**
*是否具有动态权限,理论上不需要验证
*/
privatebooleanisUserPermissionEnable(){
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
returnPackageManager.PERMISSION_GRANTED==mContext.checkSelfPermission(Manifest.permission.USE_FINGERPRINT);
}
returntrue;
}
publicvoidsetAuthListener(FingerAuthListenerauthListener){
mAuthListener=authListener;
}
publicinterfaceFingerAuthListener{
voidsuccess();
voiderror(Stringerror);
voidhelp(Stringmsg);
voidcancel();
voidfailure();
}
}
3:在main中实现FingerprintController.FingerAuthListener这个回调接口
privateintcode=FingerprintController.FINGER_SUCCESS;
privateTextViewtoast;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toast=(TextView)findViewById(R.id.toast);
code=FingerprintController.getInstance(this).checkFingerEnable();
if(code==FingerprintController.FINGER_SUCCESS){
FingerprintController.getInstance(this).setAuthListener(this);
setToast("可以启动指纹识别");
}else{
switch(code){
caseFingerprintController.FINGER_ERROR_NO_HARDWARE:
setToast("该设备不支持指纹识别");
break;
caseFingerprintController.FINGER_ERROR_NO_PERMISSION:
setToast("当前应用没有指纹识别权限");
break;
caseFingerprintController.FINGER_ERROR_NO_FINGER:
setToast("当前设备没有录入指纹,请前往录入指纹");
break;
}
}
}
publicvoidstart(Viewview){
if(code==FingerprintController.FINGER_SUCCESS){
FingerprintController.getInstance(this).startFingerAuth();
setToast("开始指纹识别");
}
}
publicvoidcancel(Viewview){
if(code==FingerprintController.FINGER_SUCCESS){
FingerprintController.getInstance(this).cancelFingerAuth();
}
}
@Override
publicvoidsuccess(){
setToast("识别成功");
}
@Override
publicvoiderror(Stringerror){
setToast(error);
}
@Override
publicvoidhelp(Stringmsg){
setToast(msg);
}
@Override
publicvoidcancel(){
setToast("取消指纹识别");
}
@Override
publicvoidfailure(){
setToast("指纹识别失败");
}
publicvoidsetToast(Stringmsg){
toast.setText("提示:"+msg);
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。