我需要在单独的线程上运行服务。这是因为它访问一个网络服务可能需要 5 到 10 秒,我不想收到 ANR。我也将绑定(bind)到此服务。我找到了一种在单独的线程中启动服务的方法,例如:
Thread t = new Thread(new Runnable() {
public void run() {
//Launch and/or Bind to service here
}
});
t.start();
但是我相信这只会在新线程中运行起始代码,而服务本身会在主线程中运行。那么我实际上如何在另一个线程中运行服务中的所有代码?
提前致谢
最佳答案
您可以使用 IntentService
IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work
http://developer.android.com/reference/android/app/IntentService.html
关于android - 如何在单独的线程上运行服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5765135/