认证成功后如何完成活动

时间:2018-07-19 15:55:01

标签: android android-studio

fingerAuthentification成功后,我正在尝试完成登录活动,我尝试使用“ finish()”和“ finishAffinity()”,但收到错误消息“无法解析器方法finish()或finishAffinity()

@TargetApi(Build.VERSION_CODES.M)
public class FingerprintHandler extends FingerprintManager.AuthenticationCallback {

    private CancellationSignal cancellationSignal;
    private Context context;

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

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

    @Override
    public void onAuthenticationError(int errMsgId, CharSequence errString) {
        Toast.makeText(context,"Authentication error\n" + errString, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onAuthenticationFailed() {
        Toast.makeText(context, "Authentication failed", Toast.LENGTH_LONG).show();
    }

    public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
        Toast.makeText(context, "Authentication help\n" + helpString, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
        Toast.makeText(context, "Authentication Success!!", Toast.LENGTH_SHORT).show();
        Handler hd = new Handler();
        hd.postDelayed(new Runnable() {
            @Override
            public void run() {
                context.startActivity(new Intent(context, MainActivity.class));
                FingerLogin.finish();
            }
        }, 2000);
    }
}

3 个答案:

答案 0 :(得分:2)

finish()Activity而非Context上的方法。您无法完成上下文。目前尚不清楚您是否在活动的上下文中运行。如果您愿意,只需给自己(或finish())打电话给YourActivity.this.finish()

答案 1 :(得分:1)

我以前没有尝试过,但是从理论上讲应该可以:

步骤1:在构造函数中传递Activity而不是Context。这是          您想要完成的活动。

第2步:将此活动分配给私有变量。

第3步:在需要时在变量上调用finish()

代码类似于:

public class FingerprintHandler extends FingerprintManager.AuthenticationCallback {

    private CancellationSignal cancellationSignal;
    private Activity activity;

    public FingerprintHandler(Activity mActivity) { activity = mActivity; }

    .....

    @Override
    public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
    Toast.makeText(activity.getApplicationContext(), "Authentication Success!!", Toast.LENGTH_SHORT).show();
    Handler hd = new Handler();
    hd.postDelayed(new Runnable() {
        @Override
        public void run() {
            activity.startActivity(new Intent(context, MainActivity.class));
            activity.finish();
        }
    }, 2000);

答案 2 :(得分:1)

使用此方法可访问您活动的覆盖方法:

           if(context instanceof FingerLogin){
                   ((FingerLogin)context).finish();
           }