难道Android的服务从一个分离式线程,而不是用户界面上运行? [英] Does Android Service run from a seperated thread instead of UI?

查看:200
本文介绍了难道Android的服务从一个分离式线程,而不是用户界面上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的是alarmmanager开始的位置张贴到HTTP服务。问题是,当管理器启动并运行该服务,用户界面​​似乎停止了一段时间。我想问问,如果服务线程从UI线程pated本身$ P $?

I am currently using a alarmmanager to start a service for posting of location to http. The problem is when the manager starts and run the services, the ui seems to stop for a while. i would to ask if the service thread is sepreated from the ui thread?

推荐答案

复制从Android的文档:

Copied From Android Docs :

注意:服务运行在其主办的主线流程的   服务不创建其自己的线程,并不在单独运行   流程(除非另行指定)。这意味着,如果   服务是打算做任何CPU密集型工作或阻塞操作   (如MP3播放或网络),你应该创建一个新的线程   在服务中做到这一点的工作。通过使用一个单独的线程,则   会降低应用程序的风险不响应(ANR)错误和   应用程序的主线程可以继续致力于用户交互   与你的活动。

Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.

<一个href="http://developer.android.com/guide/topics/fundamentals/services.html">http://developer.android.com/guide/topics/fundamentals/services.html

使用IntentService如果你不想摆弄你自己的管理线程。或者使用AsyncTasks。

Use IntentService if you dont want to fiddle with managing threads on your own. Or use AsyncTasks.

这篇关于难道Android的服务从一个分离式线程,而不是用户界面上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆