从 onhandleintent 启动异步任务 [英] start async task from onhandleintent
问题描述
我们是否应该从 IntentService
的 onHandleIntent()
方法中启动异步任务?我读到 onHandleIntent()
在 worker thread
中运行,所以从那里启动 asyncTask
安全吗??
IntentService
s 已经是后台进程;无需从那里启动 AsyncTask.此外,从任何地方启动 它是一个帮助您多线程的辅助类.如果您在 Activity 中使用它,请确保不要在 AsyncTask 的 AsyncTask
都是安全的";doInBackground()
方法中操作 View
.
如果您需要在 IntentService 中生成多个线程,只需使用:
<块引用><块引用>新线程(Runnable r).start();
查看 如何在 Android 中运行 Runnable 线程的示例?
如果您需要调用某种回调,请使用 Handler.有关示例,请参阅 http://www.vogella.com/articles/AndroidPerformance/article.html#handler
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??
IntentService
s already are background-processes; there's no need to start an AsyncTask from there. Also, starting an it's a helper class that helps you multithread. Just make sure you don't manipulate AsyncTask
is 'safe' from anywhere;View
s in the doInBackground()
-method of your AsyncTask if you use it in your Activity.
If you need to spawn multiple threads inside your IntentService, just use:
new Thread(Runnable r).start();
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
这篇关于从 onhandleintent 启动异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!