如果它在 android 中的同一个线程中运行,为什么要使用 Service

Why use Service if it runs in the same thread in android

我正在经历 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 尤其是本地服务。在那里我感到困惑。可能我理解错了。

这里是我对AndroidService的理解。您在

时创建服务
  1. 您想在后台进行单独的作业。
  2. 您想使其成为一个单独的进程。
  3. 您想在独立于启动它的组件的生命周期中实现它 运行。

混淆是列表中的第一项,背景的定义。后台不就是线程或者进程吗?我从来没有想过它可以在主线程上运行。

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

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. 是否有任何实际案例或理由可以在同一线程中将服务用作私有服务和 运行ning?

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

什么是服务

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.

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

为什么要使用服务

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

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

请阅读这篇优秀文章 understand more in detail and also read this answer

简而言之,Services运行在UI线程的后台。 您可以执行客户端-服务器身份验证等任务,也可以写入数据库,这些任务在没有图形界面的情况下在后台完成。

但是,如果您正在执行可能会冻结界面的非常长的处理任务,则可以在单独的线程上使用服务。

例如,单独线程上的服务是 IntentService

服务基本上在UI线程或thread.But主线程中运行,如果我们要在服务中执行长运行操作,我们需要创建一个后台线程并执行该任务.

但是为什么要用服务呢?

现在想想音乐 Application.We 需要歌曲即使离开音乐也要继续播放 app.If 我们使用活动,我们无法实现以上 requirement.So, 服务帮助这些场景。即使应用程序不在前台,服务也会继续 运行 并且我们能够收听 songs.This 这就是我们使用服务的原因,即使它在 main 上运行线程.