Android Service是否从单独的线程而不是UI运行? [英] Does Android Service run from a separated thread instead of UI?

查看:422
本文介绍了Android Service是否从单独的线程而不是UI运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用一个警报管理器来启动一项服务,以将位置发布到http.问题是当管理器启动并运行服务时,UI似乎会停止一段时间.我想问一下服务线程是否与ui线程分开?

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 separated 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.

服务概述

如果您不想自己管理线程,请使用IntentService.或使用AsyncTasks.

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

这篇关于Android Service是否从单独的线程而不是UI运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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