问题
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.
Is there any way to know when the user kills the app?
回答1:
there's no way to determine when a process is killed. From How to detect if android app is force stopped or uninstalled?
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.
回答2:
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));
And now whenever you will clear your app from android recent Then this method onTaskRemoved()
will execute.
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.
回答3:
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
回答4:
You can always use this code:
protected void onCreate(Bundle savedInstanceState) {
//...
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
//inform yourself that the activity unexpectedly stopped
//or
YourActivity.this.finish();
}
});
//...
}
回答5:
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:
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
}
}
回答6:
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.
来源:https://stackoverflow.com/questions/21040339/how-to-know-when-my-app-has-been-killed