我们应该从 IntentService
的 onHandleIntent()
方法中启动异步任务吗?我读到 onHandleIntent()
在 worker thread
中运行,所以从那里启动 asyncTask
是否安全??
最佳答案
IntentService
已经是后台进程;无需从那里启动 AsyncTask。 此外,从任何地方启动它是一个帮助您实现多线程的辅助类。如果您在 Activity 中使用 AsyncTask 的方法,请确保您没有在 AsyncTask
都是“安全的”;doInBackground()
中操作 View
。
如果您需要在 IntentService 中生成多个线程,只需使用:
new Thread(Runnable r).start();
请参阅 How to run a Runnable thread in Android? 中的示例
如果您需要调用某种回调,请使用 Handler .有关示例,请参阅 http://www.vogella.com/articles/AndroidPerformance/article.html#handler
关于android - 从 onhandleintent 启动异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13491049/