本文介绍了服务中的NetworkOnMainThreadException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Service类中得到一个NetworkOnMainThreadException,这从本质上讲是没有意义的,因为就我所知,服务是后台进程.

I'm getting a NetworkOnMainThreadException in my Service class, which by nature doesn't make sense, because Services are background processes as far as I understand.

在Service方法中,我正在调用静态助手方法来下载数据.我也在使用DefaultHttpClient.

In the Service method, I'm making a call to a static helper method to download data. I'm using the DefaultHttpClient as well.

这是怎么回事?

推荐答案

onStartCommand()Service中的UI线程上运行.尝试使用 IntentService ,或者在(Handler/RunnableAsyncTask).

onStartCommand() runs on the UI thread in a Service. Try using IntentService, or alternatively use any other threading method in the Service (Handler/Runnable, AsyncTask).

这篇关于服务中的NetworkOnMainThreadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 00:49