【问题标题】:Ask PIN when app comes from Background当应用来自后台时询问 PIN
【发布时间】:2017-08-01 07:18:49
【问题描述】:

我正在尝试实现一项功能,在该功能中,每次打开应用程序或来自后台时都会要求用户输入 PIN。经过搜索,我找到了a solution

我有基本的Activity 类,其余部分是从该类继承的。它被称为ParentActivity。我在ParentActivity 中实现了Foreground.Listener 并覆盖onBecameForeground,并在运行时打开PINActivity。类似下面的东西

public abstract class ParentActivity extends AppCompatActivity implements Foreground.Listener {

    ...

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

    ...

}

然后在所有子类中重写这个方法调用super

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

问题

现在的问题是,每当 App 进入前台时,此方法运行次数很少,因为有子类覆盖此方法,即使未创建子 Activity。我尝试在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);
        }
    }

}

但它仍然打开它两次。我无法弄清楚问题所在。我在这里遗漏了任何基本的 OOP 概念吗?

感谢任何帮助。谢谢。

【问题讨论】:

  • 在您的应用程序类中实现“前台”类

标签: java android oop inheritance


【解决方案1】:

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

【讨论】:

  • 我知道它解决了我的问题,但我需要知道为什么它会多次打开它。 +1 虽然
【解决方案2】:

一个非常愚蠢的错误。我在onStart() 中注册了监听器,但忘记在onStop() 中注销它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 2015-11-25
    • 1970-01-01
    • 2015-04-27
    • 2019-03-07
    • 2020-07-06
    相关资源
    最近更新 更多