【问题标题】:How to use Android Service or ServiceIntent如何使用 Android Service 或 ServiceIntent
【发布时间】:2017-11-05 01:26:10
【问题描述】:

谁能帮助我了解如何正确使用 Android Service 或 IntentService。文档在这里似乎自相矛盾:

    Caution: A service runs in the same process as the application in which it is 
declared and in the main thread of that application by default. If your service 
performs intensive or blocking operations while the user interacts with an 
activity from the same application, the service slows down activity performance. 
To avoid impacting application performance, start a new thread inside the service.

这里

public class HelloIntentService extends IntentService {

  /**
   * A constructor is required, and must call the super IntentService(String)
   * constructor with a name for the worker thread.
   */
  public HelloIntentService() {
      super("HelloIntentService");
  }

  /**
   * The IntentService calls this method from the default worker thread with
   * the intent that started the service. When this method returns, IntentService
   * stops the service, as appropriate.
   */
  @Override
  protected void onHandleIntent(Intent intent) {
      // Normally we would do some work here, like download a file.
      // For our sample, we just sleep for 5 seconds.
      try {
          Thread.sleep(5000);
      } catch (InterruptedException e) {
          // Restore interrupt status.
          Thread.currentThread().interrupt();
      }
  }
}

鉴于 Service 或 ServiceIntent 的全部目的是在后台运行长时间运行的作业而不影响 UI,为什么示例代码会完全按照 Caution 指示您不应该执行的操作 - 我假设调用 Thread .sleep() 会导致主线程阻塞。

我的以下理解是否正确:

  1. 服务本身仍然在主应用程序线程上运行,但没有 UI 组件(Activity),并且即使用户不使用应用程序也会继续运行,不像 Activity 不会
    1. 任何长时间运行的后台工作仍必须创建单独的线程以避免阻塞主应用程序线程
    2. AsyncTask 与一个 Activity 相关联,如果应用程序不再是活动应用程序(即,如果用户切换到另一个应用程序),该 Activity 可能会停止运行,这就是如果任务需要继续运行,人们会使用 Service 或 ServiceIntent 的原因。
    3. IntentService 将在与主线程不同的线程上运行任务,因此无需担心因长任务或调用 Thread.sleep() 时阻塞主线程。

我对 Android Service 或 ServiceIntents 的描述有什么误解吗?

【问题讨论】:

    标签: android multithreading


    【解决方案1】:

    您的第一条评论来自关于服务的文档一般

    Android 中没有任何名称为 ServiceIntent。您的代码 sn-p 是 IntentService

    在 Java 中,我们有类和继承。 Service 是所有服务继承的基类。 IntentService 扩展 Service

    如果你调用startService(),指向一个直接从Service继承的类,你需要自己实现一个后台线程,因为Service不会为你做这些。

    但是,如果您调用 startService(),并指向继承自 IntentService 的类,那么您获得由 IntentService 自动为您创建的后台线程。这是IntentService 添加的代码,超出了Service 提供的代码。

    我假设调用 Thread.sleep() 会导致主线程阻塞。

    不,因为在IntentService 提供的后台线程上调用了onHandleIntent()

    服务本身仍然在主应用程序线程上运行

    没有。在 Java 中,对象不在线程上运行。方法在线程上运行。

    任何长时间运行的后台工作仍然必须创建一个单独的线程以避免阻塞主应用程序线程

    任何长时间运行的后台工作都需要在后台线程上执行。这是您创建的线程(例如,在Service 的直接子类中)还是提供给您的线程(例如,在IntentService 的直接子类中)各不相同。

    AsyncTask 与一个 Activity 相关联,如果应用程序不再是活动应用程序,该 Activity 可能会停止运行

    服务的主要作用是告诉 Android 我们正在做后台工作即使应用程序的 UI 移到后台也应该持续一段时间。这是因为 Android 最终会终止您的进程,为其他应用释放系统 RAM。

    使用服务往往会使您的流程持续一段时间。 AsyncTask 或裸露的Thread 不会这样做。当应用程序的 UI 移至后台时,您的进程不会立即终止,但它可能会很快终止,因此如果您希望更好地确保您的工作能够完成,您使用服务来管理该工作。

    【讨论】:

    • 太棒了——感谢我需要确认的内容——你应该帮助编写文档。
    • @DuncanGroenewald:对不起。你必须接受a book。 :-)
    猜你喜欢
    • 2011-01-31
    • 2017-10-20
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多