当前位置:首页 > 安卓源码 > 技术博客 >

android 6.0 指纹识别调用

时间:2017-07-23 23:45 来源:互联网 作者:源码搜藏 浏览: 收藏 挑错 推荐 打印

activity_main.xml源码 [html] view plain copy ? xml version = 1.0 encoding = utf-8 ? LinearLayout xmlns:android = http://schemas.android.com/apk/res/android xmlns:tools = http://schemas.android.com/tools android:layout_width = match_parent

activity_main.xml源码

 

[html] view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:gravity="center"  
  7.     android:orientation="vertical"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  10.     android:paddingRight="@dimen/activity_horizontal_margin"  
  11.     android:paddingTop="@dimen/activity_vertical_margin"  
  12.     tools:context="com.liu.finger.MainActivity">  
  13.   
  14.     <TextView  
  15.         android:id="@+id/textView"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="Hello World!"  
  19.         android:textSize="18sp" />  
  20.   
  21.     <Button  
  22.         android:id="@+id/btn_activity_main_finger"  
  23.         android:layout_width="match_parent"  
  24.         android:layout_height="wrap_content"  
  25.         android:layout_alignParentStart="true"  
  26.         android:layout_below="@+id/textView"  
  27.         android:layout_marginTop="54dp"  
  28.         android:text="指纹识别" />  
  29. </LinearLayout>  
MainActivity.java源码

 

 

[html] view plain copy
 
  1. package com.liu.finger;  
  2.   
  3. import android.Manifest;  
  4. import android.app.KeyguardManager;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.content.pm.PackageManager;  
  8. import android.hardware.fingerprint.FingerprintManager;  
  9. import android.os.Bundle;  
  10. import android.os.CancellationSignal;  
  11. import android.support.v4.app.ActivityCompat;  
  12. import android.support.v4.app.FragmentActivity;  
  13. import android.util.Log;  
  14. import android.view.View;  
  15. import android.widget.Button;  
  16. import android.widget.Toast;  
  17.   
  18.   
  19.   
  20. public class MainActivity extends FragmentActivity {  
  21.     FingerprintManager manager;  
  22.     KeyguardManager mKeyManager;  
  23.     private final static int REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 0;  
  24.     private final static String TAG = "finger_log";  
  25.   
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.activity_main);  
  30.         manager = (FingerprintManager) this.getSystemService(Context.FINGERPRINT_SERVICE);  
  31.         mKeyManager = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);  
  32.         Button btn_finger = (Button) findViewById(R.id.btn_activity_main_finger);  
  33.         btn_finger.setOnClickListener(new View.OnClickListener() {  
  34.             @Override  
  35.             public void onClick(View v) {  
  36.                 if (isFinger()) {  
  37.                     Toast.makeText(MainActivity.this, "请进行指纹识别", Toast.LENGTH_LONG).show();  
  38.                     Log(TAG, "keyi");  
  39.                     startListening(null);  
  40.                 }  
  41.             }  
  42.         });  
  43.   
  44.     }  
  45.   
  46.     public boolean isFinger() {  
  47.   
  48.         //android studio 上,没有这个会报错  
  49.         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {  
  50.             Toast.makeText(this, "没有指纹识别权限", Toast.LENGTH_SHORT).show();  
  51.             return false;  
  52.         }  
  53.         Log(TAG, "有指纹权限");  
  54.         //判断硬件是否支持指纹识别  
  55.         if (!manager.isHardwareDetected()) {  
  56.             Toast.makeText(this, "没有指纹识别模块", Toast.LENGTH_SHORT).show();  
  57.             return false;  
  58.         }  
  59.      Log(TAG, "有指纹模块");  
  60.         //判断 是否开启锁屏密码  
  61.   
  62.         if (!mKeyManager.isKeyguardSecure()) {  
  63.             Toast.makeText(this, "没有开启锁屏密码", Toast.LENGTH_SHORT).show();  
  64.             return false;  
  65.         }  
  66.         Log(TAG, "已开启锁屏密码");  
  67.         //判断是否有指纹录入  
  68.         if (!manager.hasEnrolledFingerprints()) {  
  69.             Toast.makeText(this, "没有录入指纹", Toast.LENGTH_SHORT).show();  
  70.             return false;  
  71.         }  
  72.        Log(TAG, "已录入指纹");  
  73.   
  74.         return true;  
  75.     }  
  76.   
  77.     CancellationSignal mCancellationSignal = new CancellationSignal();  
  78.     //回调方法  
  79.     FingerprintManager.AuthenticationCallback mSelfCancelled = new FingerprintManager.AuthenticationCallback() {  
  80.         @Override  
  81.         public void onAuthenticationError(int errorCode, CharSequence errString) {  
  82.             //但多次指纹密码验证错误后,进入此方法;并且,不能短时间内调用指纹验证  
  83.             Toast.makeText(MainActivity.this, errString, Toast.LENGTH_SHORT).show();  
  84.             showAuthenticationScreen();  
  85.         }  
  86.   
  87.         @Override  
  88.         public void onAuthenticationHelp(int helpCode, CharSequence helpString) {  
  89.   
  90.             Toast.makeText(MainActivity.this, helpString, Toast.LENGTH_SHORT).show();  
  91.         }  
  92.   
  93.         @Override  
  94.         public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {  
  95.   
  96.             Toast.makeText(MainActivity.this, "指纹识别成功", Toast.LENGTH_SHORT).show();  
  97.         }  
  98.   
  99.         @Override  
  100.         public void onAuthenticationFailed() {  
  101.             Toast.makeText(MainActivity.this, "指纹识别失败", Toast.LENGTH_SHORT).show();  
  102.         }  
  103.     };  
  104.   
  105.   
  106.     public void startListening(FingerprintManager.CryptoObject cryptoObject) {  
  107.         //android studio 上,没有这个会报错  
  108.         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {  
  109.             Toast.makeText(this, "没有指纹识别权限", Toast.LENGTH_SHORT).show();  
  110.             return;  
  111.         }  
  112.         manager.authenticate(cryptoObject, mCancellationSignal, 0, mSelfCancelled, null);  
  113.   
  114.   
  115.     }  
  116.   
  117.     /**  
  118.      * 锁屏密码  
  119.      */  
  120.     private void showAuthenticationScreen() {  
  121.   
  122.         Intent intent = mKeyManager.createConfirmDeviceCredentialIntent("finger", "测试指纹识别");  
  123.         if (intent != null) {  
  124.             startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);  
  125.         }  
  126.     }  
  127.   
  128.     @Override  
  129.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  130.         if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {  
  131.             // Challenge completed, proceed with using cipher  
  132.             if (resultCode == RESULT_OK) {  
  133.                 Toast.makeText(this, "识别成功", Toast.LENGTH_SHORT).show();  
  134.             } else {  
  135.                 Toast.makeText(this, "识别失败", Toast.LENGTH_SHORT).show();  
  136.             }  
  137.         }  
  138.     }  
  139.   
  140.     private void Log(String tag, String msg) {  
  141.         Log.d(tag, msg);  
  142.     }  
  143. }  

android 6.0 指纹识别调用 转载https://www.codesocang.com/appboke/36433.html

技术博客阅读排行

最新文章