从 onhandleintent 启动异步任务 [英] start async task from onhandleintent

查看:25
本文介绍了从 onhandleintent 启动异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们是否应该从 IntentServiceonHandleIntent() 方法中启动异步任务?我读到 onHandleIntent()worker thread 中运行,所以从那里启动 asyncTask 安全吗??

解决方案

IntentServices 已经是后台进程;无需从那里启动 AsyncTask.此外,从任何地方启动 AsyncTask 都是安全的"; 它是一个帮助您多线程的辅助类.如果您在 Activity 中使用它,请确保不要在 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??

解决方案

IntentServices already are background-processes; there's no need to start an AsyncTask from there. Also, starting an AsyncTask is 'safe' from anywhere; it's a helper class that helps you multithread. Just make sure you don't manipulate Views 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆