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)

相关内容

热门资讯

透视插件!wepoker可以设... 透视插件!wepoker可以设置盖牌-关于开挂透视辅助教程(有挂教学)透视插件!wepoker可以设...
最新技巧“浙江游戏大厅修改器”... 最新技巧“浙江游戏大厅修改器”原先有开挂辅助挂(确实有挂);1、这是跨平台的浙江游戏大厅修改器黑科技...
透视挂透视!hhpoker可以... 透视挂透视!hhpoker可以开挂-揭露开挂透视辅助攻略(有挂攻略)1、hhpoker可以开挂系统规...
玩家必看分享“小程序微乐游戏辅... 玩家必看分享“小程序微乐游戏辅助器”其实有开挂辅助器(详细教程);1、让任何用户在无需AI插件第三方...
透视工具!wepoker国外版... 透视工具!wepoker国外版透视-教你开挂透视辅助工具(了解有挂)1、全新机制【wepoker国外...
专业讨论“欢乐游戏城破解版内置... 专业讨论“欢乐游戏城破解版内置修改器”本来有开挂辅助脚本(了解有挂);详细欢乐游戏城破解版内置修改器...
一分钟教你“宝宝游戏辅助”好像... 您好:宝宝游戏辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
透视最新!hhpoker透视挂... 透视最新!hhpoker透视挂实战视频-解谜开挂透视辅助攻略(有挂技术)1、超多福利:超高返利,海量...
9分钟了解“四川家园游戏辅助器... 9分钟了解“四川家园游戏辅助器平台交易”果然有开挂辅助下载(有挂规律);亲真的是有正版授权,小编(透...
透视科技!wepoker智能辅... 透视科技!wepoker智能辅助插件-教你开挂透视辅助插件(有挂方针)1、进入到wepoker智能辅...