APP Security:Android中的指纹识别

APP Security:Android中的指纹识别

什么是指纹识别

早在1684年,英国植物形态学家Grew便发表了第一篇研究指纹的科学论文,到20世纪90年代,用于个人身份鉴定的自动指纹识别系统得到开发和应用,而Android从6.0系统开始就支持指纹解锁功能,指纹解锁带给我们的便利不言而喻,现在更是有屏幕指纹解锁、人脸解锁等各种技术给我们的生活带来了极大的便利,下面是指纹解锁的一个简单实现。

具体实现

首先创建一个Empty Activity项目,我的项目名是FingerPrintDemo,在布局页面activity_main.xml中加入以下代码

<?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"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
   >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_fingerprint"
        />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入指纹"
        android:textSize="18sp" />

    <Button
        android:id="@+id/btn_activity_main_finger"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView"
        android:layout_marginTop="20dp"
        android:text="指纹识别" />
</LinearLayout>

这是一个简易的指纹验证界面,如下图所示:
在这里插入图片描述
接下来创建一个简单的指纹验证成功之后跳转的界面success.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome!"
        android:textSize="22sp" />

</LinearLayout>

验证成功的界面如下图所示:
在这里插入图片描述
然后我们需要新建一个验证指纹的Activity,我这里新建一个FingerPrintActivity类,其中onCreate方法中代码如下,当点击主界面的指纹识别按钮后,首先会进行指纹逻辑判断,若成功,则开启指纹监听进行指纹验证:

@Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      manager = (FingerprintManager) this.getSystemService(Context.FINGERPRINT_SERVICE);
      mKeyManager = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);
      Button btn_finger = (Button) findViewById(R.id.btn_activity_main_finger);
      btn_finger.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              if (isFingerPrint()) {
                  Toast.makeText(FingerPrintActivity.this, "请输入指纹", Toast.LENGTH_LONG).show();
                  Log(TAG, "keyi");
                  startListening(null);
              }
          }
      });
}

判断是否支持指纹逻辑的函数如下:

/**
   * 判断是否支持指纹逻辑
   */
public boolean isFingerPrint() {
      //判断是否具有权限
      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
          Toast.makeText(this, "抱歉,您没有指纹识别权限", Toast.LENGTH_SHORT).show();
          return false;
      }
      Log(TAG, "有指纹权限");
      //判断硬件是否支持指纹识别
      if (!manager.isHardwareDetected()) {
          Toast.makeText(this, "抱歉,您的不支持指纹识别", Toast.LENGTH_SHORT).show();
          return false;
      }
      Log(TAG, "支持指纹识别");

      //判断 是否开启锁屏密码
      if (!mKeyManager.isKeyguardSecure()) {
          Toast.makeText(this, "没有开启锁屏密码", Toast.LENGTH_SHORT).show();
          return false;
      }
      Log(TAG, "已开启锁屏密码");
      //判断是否有指纹录入
      if (!manager.hasEnrolledFingerprints()) {
          Toast.makeText(this, "抱歉,您没有录入指纹", Toast.LENGTH_SHORT).show();
          return false;
      }
      Log(TAG, "已录入指纹");

      return true;
}

如果满足进行指纹验证条件,则开启指纹识别监听,authenticate()方法接收五个参数:
1.第一个参数是CryptoObject对象,传入Cipher对象就行;
2.第二个参数是CancellationSignal对象,可以用它来取消指纹认证操作;
3.第三个参数是可选参数,官方的建议是直接传0就可以;
4.第四个参数用于接收指纹认证的回调方法,有四个回调方法;
5.第五个参数用于指定处理回调的Handler,这里直接传null表示回调到主线程;

/**
  * 开启指纹识别监听
  */
public void startListening(FingerprintManager.CryptoObject cryptoObject) {
     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);
}

四个回调方法函数如下,其主要作用在代码中已经注释:

CancellationSignal mCancellationSignal = new CancellationSignal();
    //回调方法
FingerprintManager.AuthenticationCallback mSelfCancelled = new FingerprintManager.AuthenticationCallback() {
       /**
       * 处理用户点击取消了指纹验证的逻辑
       */
       @Override
       public void onAuthenticationError(int errorCode, CharSequence errString) {
           //在多次指纹密码验证错误后,进入此方法;并且在短时间内不能调用指纹验证
           Toast.makeText(FingerPrintActivity.this, errString, Toast.LENGTH_SHORT).show();
           showAuthenticationScreen();
       }
       /**
       * 处理授权帮助的提示
       */
       @Override
       public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
           Toast.makeText(FingerPrintActivity.this, helpString, Toast.LENGTH_SHORT).show();
       }

       /**
        * 处理授权成功后跳转到主页面
        */
       @Override
       public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
           Toast.makeText(FingerPrintActivity.this, "指纹识别成功", Toast.LENGTH_SHORT).show();
           Intent intent = new Intent();
           intent.setAction("cn.itcast.START_ACTIVITY");
           startActivity(intent);
       }

       /**
        * 处理验证失败
        */
       @Override
       public void onAuthenticationFailed() {
           Toast.makeText(FingerPrintActivity.this, "指纹识别失败", Toast.LENGTH_SHORT).show();
       }
};

在多次指纹识别失败后会要求输入锁屏密码:

/**
  * 锁屏密码
  */
private void showAuthenticationScreen() {
     Intent intent = mKeyManager.createConfirmDeviceCredentialIntent("finger", "测试指纹识别");
      if (intent != null) {
          startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
      }
}

然后再创建一个指纹验证成功后的LoginActivity

public class LoginActivity extends AppCompatActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.success);
   }

}

最后在清单文件中加上以下权限就大功告成啦!

<uses-permission android:name="android.permission.USE_FINGERPRINT" />

让我们一起来看看效果吧!当指纹识别验证成功时:
在这里插入图片描述
当指纹验证失败时:
在这里插入图片描述
当多次识别失败时,需要输入锁屏密码:
在这里插入图片描述

作者:金榕榕
原文链接:https://blog.csdn.net/j_r520/article/details/90611916

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值