Android生物识别:非法状态异常。至少必须注册一个生物识别以创建需要用户认证的密钥。
创始人
2024-10-10 11:30:21
0

在Android中,您可以使用生物识别API来实现生物识别功能。如果您遇到了“非法状态异常”的问题,这可能是由于以下原因之一:没有注册任何生物识别或没有正确设置生物识别。

以下是一个示例代码,演示如何解决该问题:

首先,确保您在AndroidManifest.xml文件中添加了必要的权限:


然后,在您的Activity中,您可以使用以下代码来检查并注册生物识别:

import android.Manifest;
import android.content.pm.PackageManager;
import android.hardware.biometrics.BiometricManager;
import android.hardware.biometrics.BiometricPrompt;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

@RequiresApi(api = Build.VERSION_CODES.P)
public class MainActivity extends AppCompatActivity {

    private BiometricPrompt biometricPrompt;
    private BiometricPrompt.PromptInfo promptInfo;

    private final int REQUEST_CODE_PERMISSION = 123;

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

        // 检查是否具有生物识别权限
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_BIOMETRIC) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.USE_BIOMETRIC}, REQUEST_CODE_PERMISSION);
        } else {
            // 注册生物识别
            registerBiometrics();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == REQUEST_CODE_PERMISSION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 注册生物识别
                registerBiometrics();
            } else {
                Toast.makeText(this, "需要生物识别权限才能使用该功能", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void registerBiometrics() {
        // 创建生物识别管理器
        BiometricManager biometricManager = BiometricManager.from(this);

        // 检查生物识别状态
        if (biometricManager.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS) {
            // 创建生物识别提示
            biometricPrompt = new BiometricPrompt(this, getMainExecutor(), new BiometricPrompt.AuthenticationCallback() {
                @Override
                public void onAuthenticationError(int errorCode, CharSequence errString) {
                    super.onAuthenticationError(errorCode, errString);
                    // 处理认证错误
                }

                @Override
                public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) {
                    super.onAuthenticationSucceeded(result);
                    // 处理认证成功
                }

                @Override
                public void onAuthenticationFailed() {
                    super.onAuthenticationFailed();
                    // 处理认证失败
                }
            });

            // 创建生物识别提示信息
            promptInfo = new BiometricPrompt.PromptInfo.Builder()
                    .setTitle("生物识别认证")
                    .setSubtitle("请使用您的生物特征进行认证")
                    .setNegativeButtonText("取消")
                    .build();

            // 显示生物识别提示
            biometricPrompt.authenticate(promptInfo);
        } else {
            Toast.makeText(this, "设备不支持生物识别", Toast.LENGTH_SHORT).show();
        }
    }
}

在上面的示例中,我们首先检查是否具有生物识别权限。如果没有权限,我们请求用户授予权限。如果用户授予了权限,我们调用registerBiometrics()方法来注册生物识别。

registerBiometrics()方法中,我们首先创建生物识别管理器,并使用canAuthenticate()方法检查生物识别状态。如果状态为BIOMETRIC_SUCCESS,则说明设备支持生物识别。

然后,我们创建BiometricPrompt对象,并实现AuthenticationCallback来处理认证结果。我们还创建了PromptInfo对象来设置生物识别提示的标题、副标题和取消按钮文本。

最后,我们调用biometricPrompt.authenticate(promptInfo)

相关内容

热门资讯

记者揭秘!智星菠萝辅助(透视辅... 记者揭秘!智星菠萝辅助(透视辅助)拱趴大菠萝辅助神器,扑克教程(有挂细节);模式供您选择,了解更新找...
一分钟揭秘!约局吧能能开挂(透... 一分钟揭秘!约局吧能能开挂(透视辅助)hhpoker辅助靠谱,2024新版教程(有挂教学);约局吧能...
透视辅助!wepoker模拟器... 透视辅助!wepoker模拟器哪个好用(脚本)hhpoker辅助挂是真的,科技教程(有挂技巧);囊括...
透视代打!hhpkoer辅助器... 透视代打!hhpkoer辅助器视频(辅助挂)pokemmo脚本辅助,2024新版教程(有挂教程);风...
透视了解!约局吧德州真的有透视... 透视了解!约局吧德州真的有透视挂(透视脚本)德州局HHpoker透视脚本,必胜教程(有挂分析);亲,...
六分钟了解!wepoker挂底... 六分钟了解!wepoker挂底牌(透视)德普之星开辅助,详细教程(有挂解密);德普之星开辅助是一种具...
9分钟了解!wpk私人辅助(透... 9分钟了解!wpk私人辅助(透视)hhpoker德州透视,插件教程(有挂教学);风靡全球的特色经典游...
推荐一款!wepoker究竟有... 推荐一款!wepoker究竟有透视(脚本)哈糖大菠萝开挂,介绍教程(有挂技术);囊括全国各种wepo...
每日必备!wepoker有人用... 每日必备!wepoker有人用过(脚本)wpk有那种辅助,线上教程(有挂规律);wepoker有人用...
玩家必备教程!wejoker私... 玩家必备教程!wejoker私人辅助软件(脚本)哈糖大菠萝可以开挂,可靠技巧(有挂神器)申哈糖大菠萝...