gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:56:09 25 4
gpt4 key购买 nike

我正在经历Bound Service在 Android 开发者网站上。我以为我对服务的理解已经足够了,但我刚刚找到了另一种通过使用 Messenger 连接服务的方法。类,尤其是本地服务。在那里我感到困惑。也许我理解错了。

这里是我对Android的理解Service .您创建服务时

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

最佳答案

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

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

请阅读这篇优秀的文章到understand more in detail还有read this answer .

关于android - 如果 Service 在 android 的同一个线程中运行,为什么要使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30316906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com