0

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

Community
  • 1
  • 1
MaggotSauceYumYum
  • 402
  • 1
  • 5
  • 23

2 Answers2

2

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.

Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
  • @Toxic You can find a good tutorial at [here](http://developer.android.com/intl/es/training/scheduling/alarms.html) – Ilya Gazman Nov 08 '15 at 09:35
  • @Toxic from in the broadcast receiver class you can start an activity or a service and execute what code you like. Activity will be seen to the user. Service will not – Ilya Gazman Nov 08 '15 at 09:55
1

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.

siva
  • 1,850
  • 13
  • 14
  • i have tried that too so i have decided to stick to notification manager. i will accept your answer if you solve my problem. invite me to chat – MaggotSauceYumYum Nov 08 '15 at 12:25
  • I am using stack exchange Android app currently. May be after sometime will try to chat – siva Nov 08 '15 at 12:32