I was developping an application and I ran into a problem.
I was trying to run an asynchronous task from a service and it didn’t work. It says that the code is unreachable. Now I knwo that both run in Background, but I really want one service to control several asyncronous tasks. how can I do that?
Do I have to use threads?
I’ve read this post and I really want to use the asyncronous task:
Difference between Service, Async Task & Thread?
Also, is it possible to execute an asynchronous task from another one?
Thank you
AsyncTaskalways works on main/UI thread. And if yourServiceis running under a different process then it is not associated to main thread, that’s why you cant useAsyncTaskinService.In your case, you are encouraged to use
HandlerThread,LooperandHandlerto perform heavy lifting on separate thread.Read this article.