Using AsyncTask in Service

别来无恙 提交于 2019-12-11 05:36:58

问题


Is that a normal practice of using AsyncTask in Service (not IntentService). Or is it better to create background thread in another way?


回答1:


Is that a normal practice of using AsyncTask in Service

No. The point of AsyncTask is to be able to do some work on the main application thread after the background work is completed. Services rarely, if ever, need to do work on the main application thread.

Or is it better to create background thread in another way?

Yes: new Thread() would be a strong candidate as a straight-up replacement for AsyncTask. In other cases, some form of thread pool might make sense.




回答2:


No, an AsyncTask is designed to run some short lived work in a background thread away from the UI thread. It has callbacks that allows it to modify the UI thread once it is completed.

If you need a separate thread in a service, create a thread using new Thread()



来源:https://stackoverflow.com/questions/38468380/using-asynctask-in-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!