简体   繁体   English

当应用程序来自后台时询问PIN码

[英]Ask PIN when app comes from Background

I am trying to implement a feature in which user is asked for PIN every time it opens an App or coming from Background. 我正在尝试实现一项功能,在该功能中,每次打开应用程序或来自后台时都要求用户提供PIN码。 After searching, I found a solution . 搜索之后,我找到了解决方案

I have base Activity class from which rests are inherited. 我有从其余部分继承的基本Activity类。 It's called ParentActivity . 它称为ParentActivity I implemented Foreground.Listener in my ParentActivity and override onBecameForeground , and opening PINActivity when it runs. 我在ParentActivity实现了Foreground.Listener ,并覆盖了onBecameForeground ,并在运行时打开PINActivity Something like below 像下面这样

public abstract class ParentActivity extends AppCompatActivity implements Foreground.Listener {

    ...

    @Override
    public void onBecameForeground() {
        openSecurityActivity();
    }

    ...

}

And then override this method in all child classes to call super 然后在所有子类中重写此方法以调用super

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

PROBLEM 问题

Now the problem is whenever App comes in foreground, this method is running the no of times as there are child classes which are overriding this method, even when child Activity is not created. 现在的问题是,每当App出现在前台时,此方法就无时无刻不在运行,因为即使没有创建子Activity ,也存在覆盖该方法的子类。 I tried to put a check inside openSecurityActivity() like below, 我试图将支票放入openSecurityActivity()中,如下所示,

synchronized protected void openSecurityActivity() {
    if(securityPinRequired) {
        if(!isSecurityActivityOpened) {
            isSecurityActivityOpened = true;

            Intent intent = new Intent(this, FingerprintAndSecurityPINActivity.class);
            intent.putExtra(Constants.ASK_SECURITY_PIN, true);
            intent.putExtra("isForBackgroundVerification", true);
            startActivity(intent);
        }
    }

}

but it's still opening it twice. 但它仍然打开两次。 I can't figure out the problem. 我不知道问题所在。 Am I missing any basic OOP concept here? 我在这里错过任何基本的OOP概念吗?

Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

将android:launchMode =“ singleInstance”添加到AndroidManifest.xml文件中

A very silly mistake. 一个非常愚蠢的错误。 I registered the listener inside onStart() but forgot to unregister it inside onStop() 我在onStart()内注册了侦听器,但忘记在onStop()内取消了注册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM