An AsyncTask
, despite to popular beliefs, is not intended to be run indefinitely and really shouldn't be. AsyncTask
is meant to do short tasks that requires some effort or even network operations, and after, they just end.
If you're looking to run a loop indefinitely and you want to do it in background, I suggest using a background Service
. This doesn't provide a Thread
as AsyncTask
's doInBackground()
method does, but you can create one yourself.
As it is a Service
, you'll need to handle it accordingly and always use startService()
and stopService()
when needed, otherwise even if your app is closed it might still be running when it's not needed (which is not a good practice).