订阅或绑定到现有的Intent服务 [英] Subscribing or binding to an existing Intent service

查看:64
本文介绍了订阅或绑定到现有的Intent服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有初始活动的应用程序,该应用程序在列表视图中列出了一些文件.在列表中单击某个项目后,它将带您到该特定文件的详细活动.

I have an app that has an initial activty that list some files within a list view. When an item is clicked within the list it takes you to a detail activity of that specific file.

在详细信息视图中,我有一个名为下载的按钮,当您单击下载时,它会启动IntentService,以将要下载的文件设置为这样:

In the detail view I have a button called download, when you click on download it starts a IntentService which sets off the file to be downloaded as such:

downloadButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(AppDetailsActivity.this, AppDownloadService.class);
        intent.putExtra(Consts.APP_DOWNLOAD_RECEIVER_KEY, new DownloadReceiver(new Handler()));
        startService(intent);
    }
});

DownloadReceiver类用于更新下载文件详细活动中的进度条.

the DownloadReceiver class is used to update a progress bar within the the download file's detailed activity.

现在我想做什么...

Now what I want to do...

如果用户当前正在从detail活动中下载文件,然后返回上一个活动,则我希望该活动以某种方式订阅/绑定到相同的IntentService并检索结果,以便它可以显示进度条在要下载的文件的列表项中.

If the user is currently downloading the file from the detail activity, and then goes back to the previous activity, I want the activity to somehow subscribe/bind to the same IntentService and retrieve the results so that it can show a progress bar within the list item of the file that is downloading.

  1. 是否可以使用IntentService?
  2. IntentService是否正确?
  3. 是否有示例(因为我还没有发现任何可以显示出如何执行此操作的示例)?

推荐答案

对于下载,您可以确定使用IntentService.它在不同的线程上运行.另外,请记住,多次为该IntentService调用startService()会创建一个队列并一次处理一个意图.

For a download you sure can use an IntentService. It runs on a different thread. Also, keep in mind that calling startService() several times for that IntentService it will create a queue and will handle one intent at a time.

要显示Activity上的进度,我不相信广播是正确的事情.尽管很有可能按顺序进行处理,但是框架或Android本身不能保证在交付时就下订单.这意味着您可以获得50%的广播,然后获得40%的广播.为此,我建议您看一下以下示例: http://developer.android.com/reference/android/app/Service.html#LocalServiceSample

To show the progress on an Activity I don't belive broadcasts are the right thing. Although it is highly likely they are process in order, the framework or Android itself won't guarantee an order upon delivery. This means that you can get the broadcast for 50% and then the broadcast for 40%. For this I recommend that you take a look on this example: http://developer.android.com/reference/android/app/Service.html#LocalServiceSample

它看起来可能有些复杂,但是您将可以依靠两种方式进行通信.

It can look a little bit more complicated but you'll have a 2 way communication that you can rely on.

这篇关于订阅或绑定到现有的Intent服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆