gpt4 book ai didi

android - 一致运行两个或多个服务的最佳实践是什么?

转载 作者:行者123 更新时间:2023-11-29 22:03:11 25 4
gpt4 key购买 nike

一致运行两项或多项服务的最佳做法是什么?

例如:REST 服务检索一些数据,如果有一些新内容,下载 服务应该运行吗?

我在第一个完成后向接收器发送广播,接收器从 Intent 中获取数据并将其发送到第二个服务。

最佳答案

您也可以将 RestService 中的 Intent 直接发送到 DownloadService 而无需将 BroadcastReceiver 置于两者之间他们两个。

我认为IntentService将是您的 DownloadService 的更好方法。

IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.

All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.

编辑: 忘记提及自 API Level 9 Android 有一个 DownloadManager处理您的下载、在失败、连接更改等后重试它们的类。

关于android - 一致运行两个或多个服务的最佳实践是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11451566/

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