Stopping Background Processing (AsyncTask) when user leaves the application
I have developed an android 2.1 application. In that I am using AsyncTask for doing background processing. It gets the 开发者_开发知识库data from the WebService.
The Problem is : When I leave the application, the background service remains active only. When I go to Settings->Applications->Running Applications it shows me that the application is running. I have to forcestop that application.
Is there any way to stop the background tasks automatically when user leaves the application ?
AsyncTask
has a cancel method (http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean) )
Invoke it when your Activity is going to stopped (onStop
) state, or maybe paused - onPause
depending on your needs, see Activity lifecycle for further explanation here: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
精彩评论