Should we start async task from within onHandleIntent()
method of IntentService
? I read that onHandleIntent()
runs in worker thread
so will it be safe to start asyncTask
from there??
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
AsyncTask class is used to provide a mechanism to do achieve multithreading, so your event thread wont get hanged, but as you are using service, you should not use, AsyncTask in the Service, instead you can use, threads, if some long running task is to executed, in the Service.
If you really need to use a AsyncTask inside an IntentService, you can create a method in your AsyncTask that calls de doInBackGround and the onPostExecute. Something like this:
In my case I did this because all App request were made by a class that extended the AsyncTask, and because of the implementation was difficulty to refactor the code.
IntentService
s already are background-processes; there's no need to start an AsyncTask from there.Also, starting anit's a helper class that helps you multithread. Just make sure you don't manipulateAsyncTask
is 'safe' from anywhere;View
s in thedoInBackground()
-method of your AsyncTask if you use it in your Activity.If you need to spawn multiple threads inside your IntentService, just use:
See an example at How to run a Runnable thread in Android?
If you need to call some kind of callback, use Handler. For an example, see http://www.vogella.com/articles/AndroidPerformance/article.html#handler