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