安卓原生指紋解鎖

先看如下效果圖(圖中有幾回的失敗,不是由於它的識別率不高,是由於我用沒錄過的指紋測試):java

實現的方法很簡單,不用引入依賴,它自帶實現指紋的類,這裏我就直接貼出代碼(MainActivity.java):android

 1 public class MainActivity extends AppCompatActivity implements View.OnClickListener{
 2 
 3     private ImageView btnImg;
 4     private TextView txt;
 5     private FingerprintManager fingerprintManager = null;
 6 
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.activity_main);
11         initView();
12     }
13 
14     private void initView(){
15         btnImg = (ImageView)findViewById(R.id.btnImg);
16         btnImg.setOnClickListener(this);
17         txt = (TextView)findViewById(R.id.txt);
18     }
19 
20     @Override
21     public void onClick(View view) {
22         //隱藏點擊的圖片控件
23         btnImg.setVisibility(View.GONE);
24         //隱藏顯示的文字
25         txt.setVisibility(View.GONE);
26         //獲取系統服務對象
27         fingerprintManager = (FingerprintManager)getSystemService(Context.FINGERPRINT_SERVICE);
28         //檢測是否有指紋識別的硬件
29         if(!fingerprintManager.isHardwareDetected()){
30             Toast.makeText(this, "您的手機沒有指紋功能", Toast.LENGTH_SHORT).show();
31             return;
32         }
33 
34         //檢查設備是否處於安全狀態中
35         KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
36         if(!keyguardManager.isKeyguardSecure()){
37             //若是不是出於安全狀態中,跳轉打開安全保護(鎖屏等)
38             return;
39         }
40 
41         //檢測系統中是否註冊的指紋
42         if(!fingerprintManager.hasEnrolledFingerprints()){
43             //沒有錄入指紋,跳轉到指紋錄入
44             Toast.makeText(this, "沒有錄入指紋", Toast.LENGTH_SHORT).show();
45             return;
46         }
47 
48         //開始指紋識別
49         fingerprintManager.authenticate(null,cancellationSignal,0,myAuthCallback,null);
50     }
51 
52     //初始化cancellationSignal類
53     private CancellationSignal cancellationSignal = new CancellationSignal();
54 
55     //初始化MyAuthCallback
56     private FingerprintManager.AuthenticationCallback myAuthCallback = new FingerprintManager.AuthenticationCallback() {
57         @Override
58         public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
59             super.onAuthenticationSucceeded(result);
60             Toast.makeText(MainActivity.this, "識別成功", Toast.LENGTH_SHORT).show();
61             txt.setVisibility(View.VISIBLE);
62             txt.setText("識別成功");
63             txt.setTextColor(Color.parseColor("#01CCFF"));
64             btnImg.setVisibility(View.VISIBLE);
65         }
66 
67         @Override
68         public void onAuthenticationFailed() {
69             super.onAuthenticationFailed();
70             Toast.makeText(MainActivity.this, "識別失敗,請重試", Toast.LENGTH_SHORT).show();
71             txt.setText("識別失敗");
72             txt.setTextColor(Color.parseColor("#D81B60"));
73             txt.setVisibility(View.VISIBLE);
74         }
75     };
76 }

而後就是佈局文件的代碼:安全

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".MainActivity">
 8 
 9     <TextView
10         android:textSize="16sp"
11         android:textStyle="bold"
12         android:gravity="center"
13         android:text="指紋認證"
14         android:textColor="#FFF"
15         android:background="#01CCFF"
16         android:layout_width="match_parent"
17         android:layout_height="45dp" />
18 
19     <TextView
20         android:visibility="gone"
21         android:id="@+id/txt"
22         android:text="識別成功"
23         android:gravity="center"
24         android:textSize="20sp"
25         android:textColor="#01CCFF"
26         android:layout_width="match_parent"
27         android:layout_height="match_parent" />
28 
29     <ImageView
30         android:id="@+id/btnImg"
31         android:src="@drawable/zhiwen"
32         android:layout_marginBottom="100dp"
33         android:layout_alignParentBottom="true"
34         android:layout_centerInParent="true"
35         android:scaleType="fitXY"
36         android:layout_width="60dp"
37         android:layout_height="60dp" />
38 </RelativeLayout>
相關文章
相關標籤/搜索