Zoh*_*Ali 1 android foreground android-activity kotlin
我已经阅读了很多关于how to detect when app comes to foreground但无法找到任何令人满意的答案。他们中的大多数都在使用 onResume() 和 onClose() 方法并保持计数等
我正在开发一个加密货币应用程序,每当应用程序进入前台时,我都必须要求提供密码,这对我来说非常重要。它必须每次都要求输入密码。
所以这就是为什么我想确保默认情况下没有任何方法可以在android中检测到这一点,如果没有,那么最好的方法是什么?
现在,您可以在创建应用程序时向其添加 LifecycleObserver,以检测应用程序何时进入前台/后台。
class MyApp : Application() {
private lateinit var appLifecycleObserver : AppLifecycleObserver
override fun onCreate() {
super.onCreate()
appLifecycleObserver = AppLifecycleObserver()
ProcessLifecycleOwner.get().lifecycle.addObserver(appLifecycleObserver)
}
}
class AppLifecycleObserver() : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onEnterForeground() {
// App entered foreground
// request passpharse
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onEnterBackground() {
// App entered background
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1138 次 |
| 最近记录: |