后台工作 - 服务、线程或两者

发布于 2024-11-16 08:46:57 字数 443 浏览 4 评论 0原文

因此,我正在开发一个 Android 应用程序,作为其流程的一部分,它从网站获取数据。其中一些数据是图像。我想缓存数据。我知道我需要在后台执行此操作,但很困惑如何进行此操作。我希望每个活动都可以访问背景部分。我想我需要将此功能放入后台运行的服务中。我是否还需要创建一个新线程,或者我可以在服务中隐式执行此操作吗?

我读过有关使用 Handler 和 Looper 的内容。这些需要集成到服务中吗?或者我应该只使用那些?

编辑:

也许我不太清楚。我想从将在我的设备上显示的网页获取数据。我发现了一个“惰性列表”图像加载的示例,看起来很有希望,但有一些缺点。它设置了一个图像队列,每次下载一张图像,然后更新列表视图。但它只是一个线程。

似乎为每张图像制作一个新的图像有点毫无意义。我正在尝试修改它以允许从多个活动进行访问。我也想使用正确的结构。从阅读来看,Looper 和 Handler 似乎就是为此而设计的。

So I am developing an Android app that gets data from a website as part of its process. Some of that data is images. I want to cache the data. I know I need to do this in the background but am confused how to go about this. I want the background part to be accessible from every activity. I think I need to put this functionality into a service running in the background. Do I also need to make a new thread or can I do that implicitly in the service?

I have read about using Handler and Looper. Do these need to be integrated into the service? Or should I just use those?

EDIT:

Maybe I wasn't really clear. I want to get data from the webpage that will be displayed on my device. I found an example of "Lazy List" image loading that looks promising but had some drawbacks. It set up a queue of images it downloaded one at a time and then updated the list view. it was just a Thread though.

It seems like making a new one of these for each image would be a little pointless. I'm trying to modify it to allow access from multiple activities. I also want to use the proper constructs. From reading it seems like Looper and Handler were designed for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江湖正好 2024-11-23 08:46:57

您似乎有几个要求:

  1. 缓存来自互联网的适量数据
  2. 在后台动态获取此数据
  3. 在多个活动中使用此数据
  4. 能够处理多个并发请求

第 2 点建议如果您不想,则需要一个单独的线程影响 UI 线程的性能。第 3 点表明您需要一项服务。第 1 点和第 4 点表明某种队列可能有用。

结合所有这些的一个类是 IntentService< /a>.看看那个。它基本上在工作线程的后台排队和处理请求。由于都是互联网数据,您只需向其传递一个带有 URI 的 Intent,然后就可以让 IntentService 为您进行下载。您可能想传递 ResultReceiver其目的是,当下载完成时,您可以向 Activity 发回信号,表明您已完成该 URI。

You seem to have several requirements:

  1. Cache a moderate amount data from the internet
  2. Get this data dynamically in the background
  3. Use this data across multiple activities
  4. Be able to handle multiple concurrent requests

Point 2 suggests you need a separate thread if you don't want to impact the performance of the UI thread. Point 3 suggests you need a Service. Point 1 and 4 suggest that some sort of queue might be useful.

The one class that combines all of these is the IntentService. Take a look at that. It basically queues and handles requests in the background on a worker thread. Since it's all internet data, you can just pass it an Intent with a URI, and you can have the IntentService do the downloading for you. You probably want to pass in a ResultReceiver with the intent so that when the download completes, you can signal back to the activity that you've finished that URI.

尬尬 2024-11-23 08:46:57

目前尚不清楚您的具体用例是什么。如果您希望预加载一些图像,无论您的应用程序是否可见,都可用,您应该考虑服务。

但是,如果您只想缓存当前活动中加载的图像,AsyncTask可能是更好的选择。

在这两种情况下,您可能希望将缓存的图像存储在持久存储(缓存目录或 SQLLite)中,以便您的任何活动都可以使用它。

It's not clear what is your specific use case. If you want to preload some images to be available regardless of whether your application is visible or not, you should consider service.

However, if you just want to cache images that are loaded in your current activity, AsyncTaskmay be a better choice.

In both cases, you probably want to store your cached images in persistent store (cache directory or SQLLite), so it will be available to any of your activities.

晨光如昨 2024-11-23 08:46:57

这取决于您希望该过程的“背景”程度。如果您想在用户不知情的情况下从网站获取数据,您应该使用 Service + AsyncTask (您仍然需要 AsyncTask 因为,默认情况下,Service 与 UI 在同一线程/进程中运行——因此,如果您在没有单独线程的情况下执行任何长时间运行的任务,您将阻塞 UI 线程,并且这将是非常令人讨厌的)。

然而,这种方法会导致其他令人头痛的问题。例如,您必须找到一种方法来启动服务 - 使用 AlarmManager 定期启动它,或者找到其他一些机制(例如,侦听广播意图)。

另一种方法是在 Activity 中加载数据,同时向用户显示加载消息(您可能需要查看 ProgressDialog)。这样用户就知道您在做什么,并且您可以非常简单地实现“刷新”功能。

在这两种情况下,您都有相当多的存储选项。您可以:

  • 将检索到的文件直接存储到缓存目录/sdcard(查看 Context 上的 getCacheDir()),
  • 以防您进行解析(例如 JSON / XML)您可以将序列化对象存储到缓存目录/sdcard,这样您就不必再次解析它们
  • 您可以使用sqlite数据库(android开发网站上有文档和教程)

然后,您可以在任何以下位置使用存储的数据你的应用程序组件(活动、服务等)

It depends on how "background" you want the process to be. If you want to get the data from the website without the user even knowing about it, you should use a Service + AsyncTask (you still need the AsyncTask because, by default, the Service is run in the same thread/process as the UI -- so if you do any long-running tasks without a separate thread you'll block the UI thread and it will be quite nasty).

However, this approach will lead to other headaches. For example, you'll have to find a way to start your service -- either use the AlarmManager to start it regularly, or find some other mechanism (listen for a broadcast intent, for example).

A different approach would be to load the data in an Activity, while showing the user a loading message (you might want to look into ProgressDialog). This way the user knows what you're doing and you can very simply implement a "refresh" feature.

In both cases, you have quite a few storage options. You can:

  • store the files you retrieve directly to the cache dir / sdcard (check out getCacheDir() on Context)
  • in case you do parsing (JSON / XML for example) you can store serialized objects to the cache dir / sdcard so you don't have to parse them again
  • you can use an sqlite database (there are docs and tutorials on the android dev site)

Then, you can use the stored data in any of your application components (activities, services, etc.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文