Android 的简易指纹识别

一.第一步

1.第一步首先在build.gradle中导入咱们的指纹识别依赖。

dependencies {

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'


    implementation "androidx.biometric:biometric:1.1.0"
}

2.第二步呢就是在xml里面写一个可以弹出指纹识别的按钮,点击才可弹出指纹识别

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


        <Button
            android:id="@+id/but"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="指纹"
            android:layout_gravity="center"/>



</LinearLayout>

3. 第三步就在咱们的Activity里或Fragment里面来执行指纹的方法。下面是一个点击事件和识别的方法

//指纹登录点击事件
         button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              BiometricPrompt.PromptInfo promptInfo=newBiometricPrompt.PromptInfo.Builder()
                        .setTitle("指纹登录")
                        .setDescription("user指纹")
                        .setNegativeButtonText("取消")
                        .build();
                getprompt().authenticate(promptInfo);
            }
        });

4.这个是指纹验证的方法里面有成功的方法和失败的方法以及异常的方法

 //我这里写了一个方法,也可以不写,直接把这个里面的代码放在上面的点击事件里也是可以的
    private BiometricPrompt getprompt(){
        Executor executor = ContextCompat.getMainExecutor(this);
        BiometricPrompt.AuthenticationCallback callback=newBiometricPrompt.AuthenticationCallback() {
            //指纹验证错误
            @Override
            public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
                super.onAuthenticationError(errorCode, errString);
                Toast.makeText(MainActivity.this,errString.toString(), Toast.LENGTH_SHORT).show();
            }
            //指纹验证成功
            @Override
            public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
                super.onAuthenticationSucceeded(result);
                Toast.makeText(MainActivity.this, "指纹验证成功", Toast.LENGTH_SHORT).show();
            }
            //指纹验证失败
            @Override
            public void onAuthenticationFailed() {
                super.onAuthenticationFailed();
                Toast.makeText(MainActivity.this,"指纹验证失败", Toast.LENGTH_SHORT).show();
            }
        };
        BiometricPrompt biometricPrompt=new BiometricPrompt(this,executor,callback);
        return  biometricPrompt;
    }

4.然后就可以验证了,一定要手机支持指纹识别,要不然会吐司没有指纹传感器的的。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值