Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute code before Android app is killed

I have an Android app that connects to surrounding devices currently running the same app, even if in background.

To do this, I use WiF-Direct to advertise the fact that I am currently running said application.

Therefore I need to stop advertising this as soon as the app is killed.

  • onDestroy() cannot be used since it is not guaranteed to be called.
  • onStop() and onPause() cannot be used since the app is still running.

How can I achieve this?

Currently the service is still being advertised even when the application is closed/killed.

like image 703
Gett Avatar asked Jun 03 '16 13:06

Gett


1 Answers

You should be able to do this with a Service.

Start a service when your app was started. Override the onTaskRemoved method

@Override
public void onTaskRemoved(Intent rootIntent)
{
}

In this method do what you have to do. More detailed answer can be found here: How to know when my app has been killed? (2 answer)

like image 135
Hardcore_Graverobber Avatar answered Oct 05 '22 11:10

Hardcore_Graverobber