android - 如何检测我的应用程序何时在 android 中被强行杀死?

标签 android

如何检测我的应用何时被强行关闭。我已经尝试过一些类似服务的方式,但它无法正常工作。即使应用程序在后台,服务也会自动关闭。有没有其他方法可以在不使用服务的情况下检测应用程序关闭。

我尝试了以下解决方案但无法正常工作。 How to handle code when app is killed by swiping in android?

基本上,只有当我的应用程序被用户强行终止时,我才想注销。

最佳答案

您可以使用服务来执行此任务。

首先创建一个服务类。

通过添加 extends Service//public class MyService extends Service

然后在服务类中创建onDestroy

@Override
public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy: called");
    //Your code 

}

在AndroidManifest.xml中定义服务

<service android:name=".MyService" android:enabled="true"
        android:exported="false" />

它会很好地工作... 我已经在用了..

关于android - 如何检测我的应用程序何时在 android 中被强行杀死?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59169455/

相关文章:

android - 检查设备是否有从图库中选择 "media/*"的应用程序

Android studio 更新构建错误

android - 捕获剪贴板的粘贴事件

android - 如何使用 XML 布局作为可绘制图像

android - 如何将图像从 Android 应用程序发送到服务器?

android - 防止媒体播放器同时播放多个声音

android - intellij idea中生成覆盖方法

android - 改造。 java.net.协议(protocol)异常 : expected * bytes but received *

android - Android 中 ArrayAdapter 中的 CheckBox 和 TextView

android - 淡入淡出动画闪烁 - Android