gpt4 book ai didi

Android:检测应用程序是否进入后台

转载 作者:行者123 更新时间:2023-12-04 23:48:01 24 4
gpt4 key购买 nike

这个问题似乎有很多答案,但都不完整。一个有多个 Activity 的应用程序。我只需要一个 IF 声明意味着如果我的应用程序进入后台,我会这样做......如果它进入前台,我会这样做......

!!!请记住,如果我从一个 Activity 切换到另一个 Activity ,这意味着我的第一个 Activity 停止,这并不意味着我的应用程序进入了后台 - 它只是一个 Activity 被另一个 Activity 替换。

最佳答案

首先在app级gradle中添加如下一些必要的依赖

dependencies {
def lifecycle_version = "2.1.0"

// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
// alternatively - just ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version" // For Kotlin use lifecycle-viewmodel-ktx
// alternatively - just LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
// alternatively - Lifecycles only (no ViewModel or LiveData). Some UI
// AndroidX libraries use this lightweight import for Lifecycle
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"

annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" // For Kotlin use kapt instead of annotationProcessor
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

// optional - ReactiveStreams support for LiveData
implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version" // For Kotlin use lifecycle-reactivestreams-ktx

// optional - Test helpers for LiveData
testImplementation "androidx.arch.core:core-testing:$lifecycle_version"

}

您可以在此处引用最新版本的依赖项

https://developer.android.com/jetpack/androidx/releases/lifecycle?authuser=1

您的应用程序必须扩展 Application 类并覆盖以下示例中给出的方法。

您可以使用 Arch 库的生命周期感知组件简单地实现相同的功能。我刚刚为您的用例发布了一个 Kotlin 和 Java 示例代码。

Java 版本。
public class YourApp extends Application implements LifecycleObserver {

@Override
public void onCreate() {
super.onCreate();

ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}

// Application level lifecycle events
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onEnteredForeground() {
//Timber.d("Application did enter foreground");

}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onEnteredBackground() {
// Timber.d("Application did enter background");
}

}

Kotlin 版本:
class ZumeApp : Application(), LifecycleObserver {

override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}

// Application level lifecycle events
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onEnteredForeground() {
Timber.d("Application did enter foreground")

}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onEnteredBackground() {
Timber.d("Application did enter background")
}
}

我希望它会帮助你。如果它不起作用,请发表评论或引用 android 文档中的以下文章。

https://developer.android.com/topic/libraries/architecture/lifecycle?authuser=1

关于Android:检测应用程序是否进入后台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58808932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com