简体   繁体   中英

How to detect if the app has been killed using a service? android

How do i detect if the app has been killed? i have found a solution which is call activity on onDestroy

@Override
protected void onDestroy() {
    super.onDestroy();
    Intent intent = new Intent(this, ConnectionStablizer.class);
    startActivity(intent);
    // code here
}

it is working fine on galaxy note 3 (4.4.2) but not working on android emulator (android 4.2.2), galaxy y duos (2.3.6) and htc desire (4.0.2)

so i found another solution-

run a background service which detects when app gets destroyed and launch the activity again like this one

i have the exact implementation but it doesn't do anything? where am i wrong? my code is the same as in the link

where am I wrong

You are wrong trying to reopen your app once it's been destroyed . This is a hacky behavior...

Nevertheless, you can achieve your goal by using alarm manager and set an alarm for every second or so, to check if your app is alive if it's not you can bring it back.

Also, pay attention:

Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.

I strongly recommend to not use my solution, nor attempting to find one.

If the app is removed from recent apps by user then you can get a notification from OS by overriding onTaskRemoved method in your service and take respective action. In general Android never kills any app unless otherwise in a low memory condition. In such case callbacks will be given to save the state of your application provided you handle all such callbacks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM