Android 如何知道我的应用程序何时被杀死?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21040339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to know when my app has been killed?
提问by Darknoe
I need to know when the user kills my app (Force stop). I've been reading the android lifecycle, which has the onStop()
and onDestroy()
functions, these are related to each activity the user ends on my app, but not when the user forces stop or kills my app.
我需要知道用户何时终止我的应用程序(强制停止)。我一直在阅读 android 生命周期,它具有onStop()
和onDestroy()
功能,这些与用户在我的应用程序上结束的每个活动有关,但与用户强制停止或杀死我的应用程序时无关。
Is there any way to know when the user kills the app?
有没有办法知道用户何时杀死了应用程序?
采纳答案by Taranfx
there's no way to determine when a process is killed. From How to detect if android app is force stopped or uninstalled?
无法确定进程何时被终止。从如何检测 android 应用程序是否被强制停止或卸载?
When a user or the system force stops your application, the entire process is simply killed. There is no callback made to inform you that this has happened.
When the user uninstalls the app, at first the process is killed, then your apk file and data directory are deleted, along with the records in Package Manager that tell other apps which intent filters you've registered for.
当用户或系统强制停止您的应用程序时,整个进程都会被杀死。没有回调通知您发生了这种情况。
当用户卸载应用程序时,首先会终止进程,然后删除您的 apk 文件和数据目录,以及包管理器中的记录,这些记录告诉其他应用程序您注册了哪些意图过滤器。
回答by Faisal Khan
I have found one way to do this.....
我找到了一种方法来做到这一点......
Make one service like this
public class OnClearFromRecentService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("ClearFromRecentService", "Service Started"); return START_NOT_STICKY; } @Override public void onDestroy() { super.onDestroy(); Log.d("ClearFromRecentService", "Service Destroyed"); } @Override public void onTaskRemoved(Intent rootIntent) { Log.e("ClearFromRecentService", "END"); //Code here stopSelf(); } }
Register this service in Manifest.xml like this
<service android:name="com.example.OnClearFromRecentService" android:stopWithTask="false" />
Then start this service on your splash activity
startService(new Intent(getBaseContext(), OnClearFromRecentService.class));
做这样的一项服务
public class OnClearFromRecentService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("ClearFromRecentService", "Service Started"); return START_NOT_STICKY; } @Override public void onDestroy() { super.onDestroy(); Log.d("ClearFromRecentService", "Service Destroyed"); } @Override public void onTaskRemoved(Intent rootIntent) { Log.e("ClearFromRecentService", "END"); //Code here stopSelf(); } }
像这样在 Manifest.xml 中注册这个服务
<service android:name="com.example.OnClearFromRecentService" android:stopWithTask="false" />
然后在您的启动活动中启动此服务
startService(new Intent(getBaseContext(), OnClearFromRecentService.class));
And now whenever you will clear your app from android recent Then this method onTaskRemoved()
will execute.
现在,每当您从最近的 android 中清除您的应用程序时,onTaskRemoved()
就会执行此方法 。
NOTE: In Android O+ this solution only works when the app is full-time in foreground. After more than 1 minute with the app in background, the OnClearFromRecentService (and all other Services running) will be automatically force-killed by the system so the onTaskRemoved() will not be executed.
注意:在 Android O+ 中,此解决方案仅适用于应用程序在前台全时运行的情况。应用程序在后台运行超过 1 分钟后,系统会自动强制终止 OnClearFromRecentService(以及所有其他正在运行的服务),因此不会执行 onTaskRemoved()。
回答by rajahsekar
Create a Application class
创建应用程序类
onCreate()
Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.
onLowMemory()
This is called when the overall system is running low on memory, and actively running processes should trim their memory usage.
onTerminate()
This method is for use in emulated process environments.
Even if you are application Killed or force stop, again Android will start your Application class
即使您被应用程序杀死或强制停止,Android 也会再次启动您的应用程序类
回答by Santiago Carrillo
After digging into this problem I found a solution that might help you:
在深入研究这个问题后,我找到了一个可能对你有帮助的解决方案:
All you need to do is check on the onDestroy method of your BaseActivity that is extended by all your activities whether the last running activity of the stack is from your package or not with the following code:
您需要做的就是使用以下代码检查由您的所有活动扩展的 BaseActivity 的 onDestroy 方法,无论堆栈的最后一个运行活动是否来自您的包:
ActivityManager activityManager = (ActivityManager) getSystemService( ACTIVITY_SERVICE );
List<ActivityManager.RunningTaskInfo> taskList = activityManager.getRunningTasks( 10 );
if ( !taskList.isEmpty() )
{
ActivityManager.RunningTaskInfo runningTaskInfo = taskList.get( 0 );
if ( runningTaskInfo.topActivity != null &&
!runningTaskInfo.topActivity.getClassName().contains(
"com.my.app.package.name" ) )
{
//You are App is being killed so here you can add some code
}
}
回答by Santiago Carrillo
I have alternate idea.i have same problem like you.above methods not fix.my problem : "i want to clear all saved data entire app,when user completely close the app"
我有替代想法。我有和你一样的问题。上述方法无法解决。我的问题:“当用户完全关闭应用程序时,我想清除整个应用程序的所有保存数据”
So, i added clear() & clear saved data(from shared preference or tinyDB) in Application class.
因此,我在 Application 类中添加了 clear() 和清除保存的数据(来自共享首选项或 tinyDB)。