我想检测我的应用程序是否已最小化(我的意思是按下home键)或不按,使用Kotlin。请注意,我不是在谈论任何特定的活动或片段,我的意思是整个应用程序。我需要为应用程序假设三个独立的状态:在视图端口中显示,最小化和完全关闭。我应该如何检测前两个状态?如果android lifecycle可以做到这一点,请描述如何。谢谢!
nxagd54h1#
如果按下应用程序主页按钮,您可以在Application中使用LifecycleObserver进行检查
Application
LifecycleObserver
inner class ApplicationObserver : LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) fun onPause() { } }
inner class ApplicationObserver : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun onPause() {
}
字符串要检测何时使用刷卡和杀死您的应用程序,您可以使用服务,您可以将其注册为
ProcessLifecycleOwner .get() .lifecycle .addObserver(ApplicationObserver())
ProcessLifecycleOwner
.get()
.lifecycle
.addObserver(ApplicationObserver())
型或覆盖Activity的onUserLeaveHint方法,该方法在按下home按钮时运行。
/** * Called as part of the activity lifecycle when an activity is about to go * into the background as the result of user choice. For example, when the * user presses the Home key, {@link #onUserLeaveHint} will be called, but * when an incoming phone call causes the in-call Activity to be automatically * brought to the foreground, {@link #onUserLeaveHint} will not be called on * the activity being interrupted. In cases when it is invoked, this method * is called right before the activity's {@link #onPause} callback. * * <p>This callback and {@link #onUserInteraction} are intended to help * activities manage status bar notifications intelligently; specifically, * for helping activities determine the proper time to cancel a notification. * * @see #onUserInteraction() * @see android.content.Intent#FLAG_ACTIVITY_NO_USER_ACTION */protected void onUserLeaveHint() {}
/**
* Called as part of the activity lifecycle when an activity is about to go
* into the background as the result of user choice. For example, when the
* user presses the Home key, {@link #onUserLeaveHint} will be called, but
* when an incoming phone call causes the in-call Activity to be automatically
* brought to the foreground, {@link #onUserLeaveHint} will not be called on
* the activity being interrupted. In cases when it is invoked, this method
* is called right before the activity's {@link #onPause} callback.
*
* <p>This callback and {@link #onUserInteraction} are intended to help
* activities manage status bar notifications intelligently; specifically,
* for helping activities determine the proper time to cancel a notification.
* @see #onUserInteraction()
* @see android.content.Intent#FLAG_ACTIVITY_NO_USER_ACTION
*/
protected void onUserLeaveHint() {
型要检测用户何时向右滑动并完成应用,您可以使用Service并覆盖
Service
override fun onTaskRemoved(rootIntent: Intent?) { super.onTaskRemoved(rootIntent) setRestartAppAlarm()}
override fun onTaskRemoved(rootIntent: Intent?) {
super.onTaskRemoved(rootIntent)
setRestartAppAlarm()
型我们将此方法用于一个展示应用程序,该应用程序要求应用程序始终工作。当商店中的客户关闭应用程序时,我们设置一个闹钟并重新启动它。
1条答案
按热度按时间nxagd54h1#
如果按下应用程序主页按钮,您可以在
Application
中使用LifecycleObserver
进行检查字符串
要检测何时使用刷卡和杀死您的应用程序,您可以使用服务,您可以将其注册为
型
或覆盖Activity的onUserLeaveHint方法,该方法在按下home按钮时运行。
型
要检测用户何时向右滑动并完成应用,您可以使用
Service
并覆盖型
我们将此方法用于一个展示应用程序,该应用程序要求应用程序始终工作。当商店中的客户关闭应用程序时,我们设置一个闹钟并重新启动它。