开始从onhandleintent异步任务任务、onhandleintent

2023-09-13 02:11:18 作者:半吊子

我们应该开始异步任务从内部 onHandleIntent() IntentService 的方法是什么?我读 onHandleIntent()运行在工作线程这样才有安全地启动的AsyncTask 从那里??

解决方案

IntentService 公寓已经是后台流程;有没有必要从那里开始的AsyncTask的。 此外,启动的AsyncTask 是安全的任何地方; 这是一个辅助类,可以帮助你多线程。只要确保你不会操作查看 S在该 doInBackground() -method您的AsyncTask的,如果你用它在你的活动。

如果你需要生成你的IntentService内多个线程,只需使用:

       

新的Thread(Runnable的R)。开始();

  

请参阅在一个例子如何运行Android的一个Runnable线程?

JavaScript 执行机制 同步任务 异步任务 宏任务 微任务

如果您需要调用一些回调,使用处理器 。有关示例,请参见 http://www.vogella.com/articles/AndroidPerformance/ article.html#处理

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