Android 使用应用内身份验证时取消指纹操作

Android 使用应用内身份验证时取消指纹操作,android,android-fingerprint-api,Android,Android Fingerprint Api,我在应用程序中使用指纹管理器来验证用户在屏幕解锁后每次返回应用程序时的身份。第一次工作正常,下一次显示指纹操作已取消 这是我的密码: public class FingerprintHandler extends FingerprintManager.AuthenticationCallback { private AlertDialog.Builder dialogBuilder; private AlertDialog b; private TextView tex

我在应用程序中使用指纹管理器来验证用户在屏幕解锁后每次返回应用程序时的身份。第一次工作正常,下一次显示指纹操作已取消

这是我的密码:

public class FingerprintHandler extends FingerprintManager.AuthenticationCallback
{
    private AlertDialog.Builder dialogBuilder;
    private AlertDialog b;

    private TextView text_state;
    private ImageView image_state;
    View view;
    private Context context;

    public FingerprintHandler(Context mContext, View mView) {
        context = mContext;
        this.view = mView;
        dialogBuilder = new AlertDialog.Builder(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View dialogView = inflater.inflate(R.layout.custom_fingerprint_auth_dialog, null);
        text_state = (TextView) dialogView.findViewById(R.id.text_state);
        image_state = (ImageView) dialogView.findViewById(R.id.image_state);
       // view = inflater.inflate(R.layout.activity_passcode, null);
        dialogBuilder.setView(dialogView);
        dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.dismiss();
            }
        });
        dialogBuilder.setPositiveButton("Use Password", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                PasscodeActivity.enterPassword(view, context);
            }
        });
        b = dialogBuilder.create();
        b.show();
        b.setCancelable(false);
        b.setCanceledOnTouchOutside(false);
    }

    //Implement the startAuth method, which is responsible for starting the fingerprint authentication process//

    public void startAuth(FingerprintManager manager, FingerprintManager.CryptoObject cryptoObject) {

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

    @Override
    //onAuthenticationError is called when a fatal error has occurred. It provides the error code and error message as its parameters//

    public void onAuthenticationError(int errMsgId, CharSequence errString) {

        //I’m going to display the results of fingerprint authentication as a series of toasts.
        //Here, I’m creating the message that’ll be displayed if an error occurs//

        if (errMsgId != FINGERPRINT_ERROR_CANCELED) {
            text_state.setText(errString);
            Log.e("ERROR", "" + errString);
            image_state.setImageResource(R.drawable.ic_fingerprint_failure);
            text_state.setTextColor(context.getResources().getColor(R.color.notVerified));
            //Toast.makeText(context, "Authentication error\n" + errString, Toast.LENGTH_LONG).show();
        }
    }

    @Override

    //onAuthenticationFailed is called when the fingerprint doesn’t match with any of the fingerprints registered on the device//

    public void onAuthenticationFailed() {
        text_state.setText(context.getString(R.string.fingerFailure));
        image_state.setImageResource(R.drawable.ic_fingerprint_failure);
        text_state.setTextColor(context.getResources().getColor(R.color.notVerified));
        //Toast.makeText(context, "Authentication failed", Toast.LENGTH_LONG).show();
    }

    @Override

    //onAuthenticationHelp is called when a non-fatal error has occurred. This method provides additional information about the error,
    //so to provide the user with as much feedback as possible I’m incorporating this information into my toast//
    public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
        text_state.setText(helpString);
        Log.e("HELP",""+helpString);
        image_state.setImageResource(R.drawable.ic_fingerprint_failure);
        text_state.setTextColor(context.getResources().getColor(R.color.notVerified));
        //Toast.makeText(context, "Authentication help\n" + helpString, Toast.LENGTH_LONG).show();
    }

    @Override

    //onAuthenticationSucceeded is called when a fingerprint has been successfully matched to one of the fingerprints stored on the user’s device//
    public void onAuthenticationSucceeded(
            FingerprintManager.AuthenticationResult result) {

        text_state.setText(context.getString(R.string.fingerSuccess));
        image_state.setImageResource(R.drawable.ic_fingerprint_success);
        text_state.setTextColor(context.getResources().getColor(R.color.colorPrimaryDark));
        Activity activity = (Activity) context;
        activity.finish();
        b.dismiss();

        //Toast.makeText(context, "Success!", Toast.LENGTH_LONG).show();
    }
}

指纹操作取消通常在传感器被前一个操作占用时触发。每次您离开应用程序或完成身份验证时,请调用
cancellationSignal.cancel()这将确保传感器已完成作业,并且可以释放。在您的情况下,我会在暂停时的锁屏中添加这一行,并且在每个指纹处理程序回调中添加这一行,以确保

    @Override
//onAuthenticationFailed is called when the fingerprint doesn’t match with any of the fingerprints registered on the device//
public void onAuthenticationFailed() {
    text_state.setText(context.getString(R.string.fingerFailure));
    image_state.setImageResource(R.drawable.ic_fingerprint_failure);
    cancellationSignal.cancel();
    text_state.setTextColor(context.getResources().getColor(R.color.notVerified));
    //Toast.makeText(context, "Authentication failed", Toast.LENGTH_LONG).show();
}

如果(errMsgId!=mFingerprintManager.FINGERPRINT\u ERROR\u cancelled)它将不显示已取消的消息,则在onAuthenticationError中给出条件。我已遵循相同的内容,并且在第二次登录时面临问题,指纹操作已取消。