在服务中使用 AsyncTask

Using AsyncTask in Service

这是在服务(而非 IntentService)中使用 AsyncTask 的正常做法吗?还是以其他方式创建后台线程更好?

Is that a normal practice of using AsyncTask in Service

没有。 AsyncTask的意义在于后台工作完成后,可以在主应用线程上做一些工作。服务很少(如果有的话)需要在主应用程序线程上工作。

Or is it better to create background thread in another way?

是:new Thread() 将是 AsyncTask 的直接替代者。在其他情况下,某种形式的线程池可能有意义。

不,AsyncTask 旨在 运行 在远离 UI 线程的后台线程中进行一些短暂的工作。它具有回调,允许它在完成后修改 UI 线程。

如果您需要在服务中使用单独的线程,请使用 new Thread()

创建一个线程