How to detect application exit on android?

I don't know if that help, but if you kill your app, then service that runs in background calls method:

@Override
public void onTaskRemoved(Intent rootIntent){

    super.onTaskRemoved(rootIntent);
}

For example I had once app that was running service. When I killed app - service died too, but I wanted it to stay alive. By using onTaskRemoved I was able to schedule restart of service:

@Override
public void onTaskRemoved(Intent rootIntent){
    Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
    restartServiceIntent.setPackage(getPackageName());

    PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(
            AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime() + 1000,
            restartServicePendingIntent);

    super.onTaskRemoved(rootIntent);
}

Effect was -> I am killing my app -> Service see that tasks are being removed and calls onTaskRemoved -> I am scheduling restart of service in 1 sec -> Service dies -> After one sec it wakes up -> RESULT: App is killed, i executed code that restarted my service so it is still running in background (visible only in preferences -> applications as process)

http://developer.android.com/reference/android/app/Service.html

void     onTaskRemoved(Intent rootIntent)

This is called if the service is currently running and the user has removed a task that comes from the service's application.


My experience shows that most apps do not really need an exit-callback of the sort you are describing.

The Android way, which usually works fine, is of component-level (Activity, Service..) lifecycle management.

Some apps allow for an 'Exit' functionality (via button, menu etc.) that, when activated by the user, allows the app to close all open components and basically go down.

But, if exit-callback is really what you want, the closest thing to it would probably be to create a dedicated service with no logic other than onDestroy() function and to activate it at app startup without ever closing it!

class ExitListenerService extends Service {
    @Override
    public void onDestroy() {
        super.onDestroy();
        // App exit logic here. Not deterministic!
    }       
}

The odds are such a service will probably be the last component to be reclaimed by the operating system. This should work in most cases, it worked fine for me. But it is not 100% guaranteed.

But.. if you must have a bullet proof solution the only other way I know is to create a peer application, call it watchdog, that will periodically wake up to check weather or not your main app is still running and, if not running, will activate the exit logic.

To run this last check you will need to call

  List<RunningAppProcessInfo> runningApps = activityManager.getRunningAppProcesses();

and iterate over runningApps looking for your own.