我在NetworkOnMainThreadException
课程中收到Service
,这本质上没有意义,因为据我所知,服务是后台流程。
在Service方法中,我正在调用静态帮助器方法来下载数据。我也在使用DefaultHttpClient。
这里发生了什么?
答案 0 :(得分:5)
onStartCommand()
在Service
的UI线程上运行。尝试使用IntentService,或者使用Service
(Handler
/ Runnable
,AsyncTask
)中的任何其他线索方法。
答案 1 :(得分:0)
服务回调都在主线程上运行,也就是UI线程。如果你想做后台工作,可以启动一个Thread,或者使用IntentService的onHandleIntent(Intent i)。
答案 2 :(得分:0)
应用程序尝试执行时抛出的异常 在其主线程上进行网络操作。
这仅适用于针对Honeycomb SDK或。的应用程序 更高。允许使用早期SDK版本的应用程序 他们的主要事件循环线程上的网络,但它是很重要的 气馁。
http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
答案 3 :(得分:0)
我只是通过使用它来解决这个问题:
public int onStartCommand(Intent intent, int flags, int startId) {
RefreshDBAsync task = new RefreshDBAsync(this);
task.execute();
return START_STICKY;
}
RefreshDBAsync每隔5分钟向服务器发出一次请求