android sdk中的描述

Caution:A

service runs in the main thread of its hosting process—the service

doesnotcreate

its own thread and

doesnotrun

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.

service是运行在主线程上的,而不是运行在另一个线程中,如果你想在service中处理很占时间的操作,你必须在service中开线程,这样可以降低activity没有响应的风险。

Should you use a service or a thread?

A service is simply a component that can run in the background

even when the user is not interacting with your application. Thus,

you should create a service only if that is what you need.

If you need to perform work outside your main thread, but only

while the user is interacting with your application, then you

should probably instead create a new thread and not a service. For

example, if you want to play some music, but only while your

activity is running, you might create a thread inorProcesses

and Threadingdocument for more

information about threads.

Remember that if you do use a service, it still runs in your

application's main thread by default, so you should still create a

new thread within the service if it performs intensive or blocking

operations.

摘自:http://blog.csdn.net/cwx01perfect/article/details/7522816

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐