How to call webservice (Http Request) when app is killed by swiping in android?

3.1k views Asked by At

Question/Requirement

How to call Web-Service (HTTP Request) when app is killed by swiping in android?

When I kill application by swiping from recent application list at that time I want to call Web-Service (or AsyncTask).

What I used?

I am trying to call AsyncTask on destroy of activity.

AsyncTask contains WebApi request (using DefaultHttpClient).

Code like this...

@Override
protected void onDestroy() {
    String url = "<webapi-url here>";
    new GetResponse().execute(url);
    super.onDestroy();
}

I am not using any background service.

Used only Activities, not containing any Fragment.

If any solution then please share here.

3

There are 3 answers

1
siva On BEST ANSWER

You can write an Android service component which overrides a method called onTaskRemoved() which will be triggered whenever app is removed by swiping from recents. So you can try this solution and see it fulfills your requirement.

0
shkschneider On

No, there's no reliable way to know if your application was killed by a another process. The whole point of "killing" an app is to terminate it as soon as possible, without letting it run any code.

If you want more, see Action on application killed in android

2
Merlí Escarpenter Pérez On

You have a similar post, the next time search a little please, but you can see this posts: AsyncTask will always run even if app is destroyed? or Async task stopped when App terminates (onDestroyed is called).

In summary the AsyncTask works when app is destroyed but the app can crash if you try to modify UI. Tell me if I helps you, good programming!