Search code examples
javaandroidandroid-studioandroid-intentfingerprint

Using Intent when Fingerprint is recognized


I'm currently developing an Application that has a MainActivity which consists in a Fingerprint security system, the idea of the app is that, when fingerprint recognized, the app should open a different activity.

All of my fingerprint checking stuff is concentrated in a Java.class called FingerprintHandler, which you can see below:

@TargetApi(Build.VERSION_CODES.M)

public class FingerprintHandler extends FingerprintManager.AuthenticationCallback {

// Usamos CancellationSignal para que una vez fuera de la App otras apps puedan usar el sensor de huellas sin problemas

private CancellationSignal cancellationSignal;
private Context context;

public FingerprintHandler(Context mContext) {
    context = mContext;
}

// Implementamos startAuth ya que es el encargado de realizar la autenticación de la huella digital
public void startAuth(FingerprintManager manager, FingerprintManager.CryptoObject cryptoObject) {

    cancellationSignal = new CancellationSignal();
    if (ActivityCompat.checkSelfPermission(context, permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    manager.authenticate(cryptoObject, cancellationSignal, 0, this, null);
}

@Override
// onAuthtenticationError es llamado cuando ocurre un error fatal, y nos proporciona información sobre este
public void onAuthenticationError(int errMsgId, CharSequence errString) {
    Toast.makeText(context, "Error de autenticación\n" + errString, Toast.LENGTH_SHORT).show();
}

@Override
// onAuthenticationHelp es llamado cuando ocurre un error no fatal, y nos proporciona información sobre este
public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
    Toast.makeText(context, "Ayuda de autenticación:\n " + helpString, Toast.LENGTH_SHORT).show();
}

@Override
// onAuthenticationSucceeded es llamado cuando la huella utilizada coincide con alguna de la sregistradas en el dispositivo
public void onAuthenticationSucceeded(AuthenticationResult result) {
    Toast.makeText(context, "Autenticación realizada con éxito", Toast.LENGTH_SHORT).show();

}

@Override
// onAuthenticationFailed es llamado cuando la huela dactilar utilizada no coincide con ninguna de las registradas en el dispositivo
public void onAuthenticationFailed() {
    Toast.makeText(context, "La huella no coincide con ninguna de las registradas", Toast.LENGTH_SHORT).show();
}

}

I tried to set an Intent on the method onAuthenticationSucceeded, but it doesn´t work, this is basically the intent I tried to set inside onAuthenticationSucceeded:

Intent intent = new Intent(MainActivity.class, CommandSenderActivity.class);
    startActivity(intent);

The thing is, my Android Studio says that he cannot resolve the Intent constructor, and also, "startActivity" isn't recognized, it appears red.

So that's basically my doubt, I need an intent that leads me from MainActivity to CommandSenderActivity, but I need to do it when the fingerprint is correctly checked (that's why I tried to build the Intent inside of onAuthenticationSucceeded). What can be my error here?

by the way, thank you for having the patience to help newbies such as me, you veterans are the best here!!!

(Oh, and ignore the anotations, those are in Spanish, just ignore them :p )


Solution

  • The startActivity(intent); it is a method of Context class, so you can use a context variable in your FingerprintHandler class:

    Intent intent = new Intent(context, CommandSenderActivity.class);
    context.startActivity(intent);