如果在 android 的同一线程中运行,为什么要使用 Service [英] Why use Service if it runs in the same thread in android

查看:35
本文介绍了如果在 android 的同一线程中运行,为什么要使用 Service的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Android 开发者网站上浏览 绑定服务.我以为我已经足够了解该服务,但我刚刚通过 Using a Messenger 类,专门用于本地服务.在那里我感到困惑.也许我理解错了.

I was going through Bound Service in Android Developer website. I thought I understood the service enough but I just found another way of connecting service through Using a Messenger class especially for local service. There I got confused. Maybe I got the concept wrong.

这里是我对Android的理解Service.您在

Here is my understanding of Android Service. You create a service when

  1. 您想在后台中做不同的工作.
  2. 您想让它成为一个单独的过程.
  3. 您想让它在独立于启动它的组件的生命周期中运行.

混淆是列表中的第一项,背景的定义.后台不是线程或进程吗?没想到可以在主线程上运行.

Confusion is the first item in the list, the definition of the background. Isn't the background a thread or process? I never thought that it can run on the main thread.

这里是开发页面中关于服务的注意事项.

Here is the caution of service in the dev pages about.

注意:服务在其托管进程的主线程中运行——该服务不会创建自己的线程,也不会在单独的进程中运行(除非您另行指定).这意味着,如果您的服务要执行任何 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.

问题

  1. 如果服务功能无论如何都会在主线程上运行,为什么要选择使用服务?
  2. 即使耗时的工作在主线程中完成,我们是否必须编写一个服务只阻止 ANR?假设该服务仅适用于我的应用程序.
  3. 是否有任何实际案例或理由将服务用作私有服务并在同一线程中运行?

推荐答案

应用程序主线程并不总是 UI 线程.例如,当 Activity 停止时,onStop() 被调用,因此 UI 线程从那个 Activity 被带走并移动到另一个相同或不同应用程序中的活动.然而,这并不意味着应用程序不再处于活动状态,它可以继续在后台工作,直到它被操作系统或用户关闭.那么谁让它在后台运行呢?它是主线程,而不是 UI 线程.

Application main thread is not always the UI thread. For example, when Activity is stopped, the onStop() is invoked, hence the UI thread is taken away from that Activity and moved to another Activity within the same or a different application. However it doesn't mean the application is no longer active, it can continue working in the background until it is closed either by OS or by user. Then who keeps it running in the background? It is the main thread and not the UI thread.

什么是服务

在 Android 中,Service 是一个应用程序组件,可以执行在 UI 线程的后台长时间运行的操作.经过背景,这意味着它没有用户界面.服务默认在调用组件进程的主线程上运行(因此会降低响应能力并导致 ANR),因此您应该创建一个新线程来执行长时间运行的操作.一个还可以使服务在完全不同的进程中运行.

In Android, a Service is an application component that can perform long-running operations in the background on the UI thread. By background, it means that it doesn’t have a user interface. A Service runs on the main thread of the calling Component’s process by default (and hence can degrade responsiveness and cause ANRs), hence you should create a new Thread to perform long running operations. A Service can also be made to run in a completely different process.

与 Activity 组件不同,Services 没有任何图形接口.广播接收器也用于接收广播消息(广播、多播、单播)并执行短任务而服务旨在进行冗长的处理,例如流式处理音乐、网络事务、文件 I/O、与数据库交互等.当服务由应用程序组件(如活动)启动时它在后台运行并保持运行,即使用户切换到另一个应用程序或启动组件本身被破坏

Unlike Activity components, Services do not have any graphical interfaces. Also Broadcast Receivers are for receiving broadcast messages (broadcast, multicast, unicast) and perform short tasks whereas Services are meant to do lengthy processing like streaming music, network transactions, file I/O, interact with databases, etc. When a Service is started by an application component like an Activity it runs in the background and keeps running even if the user switches to another application or the starting component is itself destroyed

为什么要使用服务

服务的优先级高于其他后台进程,并且因此,Android 终止它的可能性较小.虽然可以配置为在有足够的可用资源后重新启动再次.您应该经历不同的过程及其流程和文档中的优先级/重要级别线程.为它们分配与前台活动相同的优先级是绝对有可能在这种情况下它需要有一个可见的通知处于活动状态(通常用于播放音乐的服务).

Services are given higher priority than other Background processes and hence it’s less likely that Android will terminate it. Although it can be configured to restart once there is ample resources available again. You should go through the different processes and their priority/important level in the documentation on processes and threads. Assigning them the same priority as foreground activities is definitely possible in which case it’ll need to have a visible notification active (generally used for Services playing music).

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

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

请阅读这篇优秀文章以了解更多细节并且阅读此答案.

Please read this excellent article to understand more in detail and also read this answer.

这篇关于如果在 android 的同一线程中运行,为什么要使用 Service的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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