简体   繁体   English

我如何检测我的 android 应用程序是否被销毁/暂停/停止而不会失败?

[英]How can I detect If my android application is Destroyed/Paused/Stopped without fail?

I want to update user activity status (Online/Offline) based on Application is running or not.我想根据应用程序是否运行来更新用户活动状态(在线/离线)。 To do so I have used LifecycleEventObserver to Observe the lifecycle inside the Application file .为此,我使用LifecycleEventObserver来观察应用程序文件中的生命周期。

@Override
public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
    Log.e("onStateChanged", "Lifecycle.Event : " + event.name());
    switch (event.name()) {
        case "ON_START":
        case "ON_RESUME":
            setStatus(true);
            break;
        case "ON_STOP":
        case "ON_PAUSE":
            setStatus(false);
            break;
    }
}

It works fine in happy flow as shown in this .Gif file (When switching to recent apps it logs Lifecycle events ON_PAUSE, ON_STOP inside onStateChanged ).如此.Gif 文件所示,它在快乐流程中运行良好(当切换到最近的应用程序时,它会在onStateChanged内记录生命周期事件 ON_PAUSE、ON_STOP)。

  • Issue case:问题案例:

But I have found one case in which the application fails to observe when the application stops running .但是我发现了一种应用程序无法观察应用程序何时停止运行的情况 As shown in this .Gif file (When switching to recent apps and the user kills an application from recent apps rapidly, it does not get a chance to observe the lifecycle events. So here, It is not able to log Lifecycle events ON_PAUSE, ON_STOP inside onStateChanged ).如这个.Gif文件所示(当切换到最近的应用程序并且用户从最近的应用程序中快速杀死一个应用程序时,它没有机会观察生命周期事件。所以这里,它无法记录生命周期事件ON_PAUSE,ON_STOP在onStateChanged内)。

I have also tried DefaultLifecycleObserver but it also behaves the same way.我也尝试过DefaultLifecycleObserver但它的行为方式也相同。

@Override
public void onStart(@NonNull LifecycleOwner owner) {
    Log.e("Globals", "onStart");
    setStatus(true);
}

@Override
public void onCreate(@NonNull LifecycleOwner owner) {
    Log.e("Globals", "onCreate");
}

@Override
public void onResume(@NonNull LifecycleOwner owner) {
    Log.e("Globals", "onResume");
    setStatus(true);
}

@Override
public void onPause(@NonNull LifecycleOwner owner) {
    Log.e("Globals", "onPause");
    setStatus(false);
}

@Override
public void onStop(@NonNull LifecycleOwner owner) {
    Log.e("Globals", "onStop");
    setStatus(false);
}

@Override
public void onDestroy(@NonNull LifecycleOwner owner) {
    Log.e("Globals", "onDestroy");
    setStatus(false);
}

Unfortunately, this behavior is by design.不幸的是,这种行为是设计使然。 When the user kills the app from the Recents menu, it's the same as force-stopping it from the phone's settings -- it's not meant to be graceful, and the usual lifecycle methods aren't called.当用户从“最近”菜单中终止应用程序时,它与从手机设置中强制停止应用程序相同——这并不意味着是优雅的,并且不会调用通常的生命周期方法。

But on the broader subject of trying to detect if the app is actually running, you can just implement a "heartbeat" signal that the app sends out periodically (either to a web server, or to another app).但在尝试检测应用程序是否实际运行的更广泛主题上,您可以实现应用程序定期发送的“心跳”信号(发送到 web 服务器或另一个应用程序)。 And if that heartbeat signal stops being received, then the app has been shut down如果停止接收该心跳信号,则该应用程序已关闭

暂无
暂无

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

相关问题 当我的应用程序暂停/杀死/销毁时,倒计时计时器是否已停止/销毁? - Are countdowntimers stopped/destroyed when my App is paused/killed/destroyed? 如何在没有错误的情况下启动Android SQLite应用程序 - How I can start my Android SQLite Application without a Error android crashlytics检测应用程序突然被破坏并消失了 - android crashlytics to detect application suddenly destroyed and disappeared 在Java中,如果我的程序没有-ea运行怎么办? - In Java, how can I fail if my program is run without -ea? 如何保存我的Android应用程序,以便最终用户可以在没有Android SDK的情况下执行它 - How can I save my Android application so that the end-user can execute it without the Android SDK 如何检测用户是否是第一次使用我的应用程序? - How can I detect if a user is using my application for the first time? 如何从库中检测已暂停(未终止)和已恢复(未创建)的应用程序 - How to detect Application Paused(Not killed) and Resumed(Not created) from a library 如何检测Android中外部应用程序的点击事件? - How can I detect click events of external application in Android? 当活动被破坏/暂停时,我应该删除任何监听器吗? Android工作室 - Should I remove any listeners when activity is destroyed/paused? Android Studio Android 如何检测 java 中的应用程序卸载 - Android How I can Detect application uninstallation in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM