服务器中的android.os.NetworkOnMainThreadException在一个单独的进程中

前端之家收集整理的这篇文章主要介绍了服务器中的android.os.NetworkOnMainThreadException在一个单独的进程中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在ICS上,我在使用UrlConnection时遇到android.os.NetworkOnMainThreadException错误 – 即使我在一个运行在它自己的进程上的服务中发出此请求,并且被异步调用以通过Messenger完成.

更改StrictPolicy无效,我仍然收到错误.

我能做什么?

编辑:此服务在一个单独的进程中运行 – 具有不同的pid和所有内容.

最佳答案
根据documentation,服务仍在主线程上运行:

Note that services,like other application objects,run in the main thread of their hosting process. This means that,if your service is going to do any cpu intensive (such as MP3 playback) or blocking (such as networking) operations,it should spawn its own thread in which to do that work. More information on this can be found in Processes and Threads. The IntentService class is available as a standard implementation of Service that has its own thread where it schedules its work to be done.

因此,对于长时间运行的任务(例如使用UrlConnection),您应该使用类似IntentService的东西或自己生成新的线程.

原文链接:https://www.f2er.com/android/430586.html

猜你在找的Android相关文章