难道Android的服务从一个分离式线程,而不是用户界面上运行?线程、用户界面、而不是、Android

2023-09-12 07:56:55 作者:可念不可及

我目前使用的是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.

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.