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

查看:20
本文介绍了Android 服务是否从单独的线程而不是 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?

推荐答案

Copied From Android Docs :

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 服务是否从单独的线程而不是 UI 运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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