skip to Main Content

So as a homework from collage i have to make a simple login app using fingerprint, i followed this video: How to Make a FingerPrint Authentication System in Android Studio and Java

But im getting some error that aren’t explained and i can’t find a answer form, this is my code:

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);

            }
        });


    }
}

At BiometricManager biometricManager = BiometricManager.from(this); im getting "cannot resolve method ‘from’ in ‘biometricmanager’", but every video i have seen says that you create your BiometricManager this way.

At BiometricPrompt biometricPrompt = new BiometricPrompt(MainActivity.this, executor, new androidx.biometric.BiometricPrompt.AuthenticationCallback() { im getting "’BiometricPrompt’ has private access in ‘android.hardware.biometrics.BiometricPrompt’".

At final BiometricPrompt.PrompInfo prompInfo = BiometricPrompt.PrompInfo.Builder() im getting "cannot resolve PromptInfo"

And finally at biometricPrompt.authenticate(prompInfo); im getting "cannot resolve method ‘(authenticate(BiometricPrompt.PrompInfo))’"

2

Answers


  1. Chosen as BEST ANSWER

    Sorry for the inconvinience, i fount out what was wrong, instead of using:

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

    use

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

    in the build.gradle i used

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

    But this can depend on new updates, so try with the newer ones also: https://mvnrepository.com/artifact/androidx.biometric/biometric?repo=google

    And finally i had PrompInfo instead of PromptInfo

    This other post really helped me: Android BiometricPrompt: Cannot resolve symbol PromptInfo


    1. firstly add dependency

      implementation "androidx.biometric:biometric:1.2.0-alpha05"
      
    2. then

      BiometricManager biometricManager = androidx.biometric.BiometricManager.from(this);
      

    Thands

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search