Android Studio :如何创建BiometricManager和其他错误

smtd7mpg  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(208)

因此,作为一个作业,从拼贴我必须使一个简单的登录应用程序使用指纹,我跟随这个视频:How to Make a FingerPrint Authentication System in Android Studio and Java
但我得到一些错误,没有解释,我找不到答案的形式,这是我的代码:

package com.example.actividad14;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.graphics.Color;
import android.hardware.biometrics.BiometricManager;
import android.hardware.biometrics.BiometricPrompt;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.concurrent.Executor;

public class MainActivity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView msg_txt = findViewById(R.id.txt_msg);
        Button login_btn = findViewById(R.id.login_btn);

        BiometricManager biometricManager = BiometricManager.from(this);
        switch (biometricManager.canAuthenticate()){
            case BiometricManager.BIOMETRIC_SUCCESS:
                msg_txt.setText("You can use the fingerprint sensor to login");
                msg_txt.setTextColor(Color.parseColor("#Fafafa"));
                break;
            case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
                msg_txt.setText("The device doesn't have a fingerprint sensor");
                login_btn.setVisibility(View.GONE);
                break;
            case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
                msg_txt.setText("The biometric sensor is currently unavailable");
                login_btn.setVisibility(View.GONE);
                break;
            case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
                msg_txt.setText("Your device doesn't have any fingerprint saved, please check your security settings");
                login_btn.setVisibility(View.GONE);
                break;

        }

        Executor executor = ContextCompat.getMainExecutor(this);

        BiometricPrompt biometricPrompt = new BiometricPrompt(MainActivity.this, executor, new androidx.biometric.BiometricPrompt.AuthenticationCallback() {
            @Override
            public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
                super.onAuthenticationError(errorCode, errString);
            }

            @Override
            public void onAuthenticationSucceeded(@NonNull androidx.biometric.BiometricPrompt.AuthenticationResult result) {
                super.onAuthenticationSucceeded(result);
                Toast.makeText(getApplicationContext(),"Login Succes!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAuthenticationFailed() {
                super.onAuthenticationFailed();
            }
        });

        final BiometricPrompt.PrompInfo prompInfo = BiometricPrompt.PrompInfo.Builder()
                .setTitle("Login")
                .setDescription("Use your fingerprint to login in your app")
                .setNegativeButtonText("Cancel")
                .build();

        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                biometricPrompt.authenticate(prompInfo);

            }
        });

    }
}

BiometricManager biometricManager = BiometricManager.from(this);,我得到了“无法解析”biometricmanager“中的方法”from“”,但是我看到的每个视频都说你是这样创建你的BiometricManager的。
BiometricPrompt biometricPrompt = new BiometricPrompt(MainActivity.this, executor, new androidx.biometric.BiometricPrompt.AuthenticationCallback() {我得到“'生物统计提示'有私人访问'android.硬件.生物统计.生物统计提示'”。
final BiometricPrompt.PrompInfo prompInfo = BiometricPrompt.PrompInfo.Builder()时,im得到“无法解析提示信息”
最后,在biometricPrompt.authenticate(prompInfo);处,我得到“无法解析方法'(authenticate(BiometricPrompt. PromInfo))'”

qybjjes1

qybjjes11#

很抱歉给您带来不便,我找出了问题所在,而不是使用:

import android.hardware.biometrics.BiometricManager;
import android.hardware.biometrics.BiometricPrompt;

使用了

import androidx.biometric.BiometricManager;
import androidx.biometric.BiometricPrompt;

我用的是Gradle

implementation 'androidx.biometric:biometric:1.2.0-alpha04'

但这可能取决于新的更新,所以也尝试使用较新的更新:https://mvnrepository.com/artifact/androidx.biometric/biometric?repo=google
最后我用提示信息代替了提示信息
另一个帖子对我很有帮助:Android BiometricPrompt: Cannot resolve symbol PromptInfo

相关问题