指紋識別作為最新興起的用戶身份驗證機制,已經(jīng)被越來越多的應(yīng)用程序所采用,相比傳統(tǒng)的密碼九宮格等驗證方法,指紋識別更加安全,如今越來越多的安卓手機配備了指紋識別模塊,我們就來嘗試一下這一項新興的技術(shù)
首先創(chuàng)建一個安卓項目,指紋識別作為最近幾年才興起的技術(shù),對安卓api level要求較高(api 23以上即jdk6.0以上)
創(chuàng)建一個button用于觸發(fā)指紋識別,并在MainActivity里對其監(jiān)聽,代碼如下
1
2
3
4
5
6
7
8
|
Button button=(Button)findViewById(R.id.checkFingerPrint); button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { Log.d( "btn" , "開始識別" ); checkFingerPrint(); } }); |
在checkFingerPrint方法里調(diào)用系統(tǒng)提供的指紋識別函數(shù)FingerprintManagerCompat的authenticate,代碼如下
1
2
3
|
private void checkFingerPrint(){ FingerprintManagerCompat.from( this ).authenticate( null , 0 , null , new MyCallBack(), null ); } |
在這里解釋一下這個方法里各個參數(shù)的含義
第一個參數(shù)是用于通過指紋驗證取出AndroidKeyStore中的key的對象
第二個參數(shù)可以用來取消指紋驗證,如果想手動關(guān)閉驗證,可以調(diào)用該參數(shù)的cancel方法
第三個參數(shù)沒什么意義,就是傳0就好了
第四個參數(shù)最重要,由于指紋信息是存在系統(tǒng)硬件中的,app是不可以訪問指紋信息的,所以每次驗證的時候,系統(tǒng)會通過這個callback告訴你是否驗證通過、驗證失敗等
第五個參數(shù)是handler,fingerprint中的消息都通過這個handler來傳遞消息,如果你傳空,則默認創(chuàng)建一個在主線程上的handler來傳遞消息,沒什么用,0傳null好了
通過這個函數(shù)api我們能夠看出來第四個參數(shù)最重要,他是決定指紋成功和失敗以及驗證失敗后的后續(xù)操作,接下來我們就通過繼承重寫這個函數(shù)(注意參數(shù)一定要跟父類一致,博主就是沒寫對一開始函數(shù)不執(zhí)行),代碼如下
1
2
3
4
5
6
7
8
9
10
|
public class MyCallBack extends FingerprintManagerCompat.AuthenticationCallback{ private static final String TAG = "MyCallBack" ; @Override public void onAuthenticationFailed(){ Log.d(TAG, "指紋錯誤" ); } public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result){ Log.d(TAG, "指紋正確" ); } } |
這里我們給指紋識別正確和錯誤添加了打印在控制臺的后續(xù)動作,當然繼承的這個類還有其他的方法onAuthenticationError和onAuthenticationHelp,這兩個方法是處理識別不成功的。這里要區(qū)分一下識別不成功和識別錯誤的區(qū)別,前者是指沒有識別到指紋而后者是識別到了指紋但不匹配
接下來我們用模擬器做測試,出于安全的考慮谷歌暫時沒有開放指紋存儲的功能,因此該功能使用的是系統(tǒng)里的指紋,也就是我們傳統(tǒng)的用于解鎖屏幕時設(shè)置的指紋,為了測試我們?nèi)ツM器里設(shè)置一個指紋
接下來進到程序里面去測試我們的指紋識別,首先是用已經(jīng)錄入的指紋,通過控制臺可以看到已經(jīng)執(zhí)行了識別成功的方法
然后我們換錯誤的指紋,再試一次,可以看到錯誤指紋的方法也已經(jīng)被調(diào)用了
接下來貼出全部的代碼供大家參考
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package com.example.john.fingerprinttest; import android.support.v4.hardware.fingerprint.FingerprintManagerCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button=(Button)findViewById(R.id.checkFingerPrint); button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { Log.d( "btn" , "開始識別" ); checkFingerPrint(); } }); } private void checkFingerPrint(){ FingerprintManagerCompat.from( this ).authenticate( null , 0 , null , new MyCallBack(), null ); } public class MyCallBack extends FingerprintManagerCompat.AuthenticationCallback{ private static final String TAG = "MyCallBack" ; @Override public void onAuthenticationFailed(){ Log.d(TAG, "指紋錯誤" ); } public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result){ Log.d(TAG, "指紋正確" ); } } } |
這個程序只實現(xiàn)了簡單的指紋識別,當然作為一個涉及到安全性的功能,谷歌提供的東西遠不止這些,比如多次識別錯誤的保護等等。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/qq_28377423/article/details/72868495