2

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.

shkschneider
  • 17,833
  • 13
  • 59
  • 112
Darshak
  • 2,298
  • 4
  • 23
  • 45

3 Answers3

4

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.

siva
  • 1,850
  • 13
  • 14
  • for < 14 platforms, you can try a weird logic where a service will wakeup for every 5sec or so and will query the running taks info. If your app is not present in the running task info then you can send message using webservice. this service will run as a different app and this logic works only on older platforms upto kitkat.This is not a recommended approach as it drains your battery. – siva Jun 12 '15 at 13:19
0

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

shkschneider
  • 17,833
  • 13
  • 59
  • 112
0

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!

Community
  • 1
  • 1
  • The other way it's using intent service!! :) – Merlí Escarpenter Pérez Jun 11 '15 at 14:24
  • The intent service it's a service app if app crashed or is destroyed,the intent service never die... But i think it's a lot of work to do a httprequest, sit and think it's a good idea before to complicate the code. All apps sync a lot of times before die. Sync before to destroy app it's not necessary in 95% of cases, you can sync in OnResume, OnPause (this times is when app is coming to first plane) or when user navigates in the UI, for example... – Merlí Escarpenter Pérez Jun 11 '15 at 14:41