简体   繁体   中英

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

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.

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.

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM