AsyncTask的在多个活动? [英] AsyncTask over Multiple Activities?

查看:92
本文介绍了AsyncTask的在多个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,伙计们,我只能找到1个线程这一点,并没有真正回答我的问题,所以我要求我自己。

Hey guys, I could only find 1 thread on this, and it didn't really answer my question, so I'm asking for myself.

我的应用程序被下载和解析一个RSS文件,我已经决定显然为此在后台任务。我选择的AsyncTask,而我试图让它开始下载,然后弹出的列表视图中的RSS提要。从那里,我想从另一个活动的AsyncTask的,因为它装载它的现在解析的信息传递到新的列表视图活性。我无法弄清楚如何通过引用我的AsyncTask对象到我的新活动,而无需实现parcelable并将其添加到这将是一个巨大的混乱,我认为捆绑。有没有更好的方式来做到这一点?

My application is downloading and parsing an rss file, and I've obviously decided to do this in a background task. I chose AsyncTask, and I'm trying to get it to start the download and then bring up the list view for the rss feed. From there I'd like the AsyncTask from the other activity to pass the now parsed information to the new list view activity as it loads it. I can't figure out how to pass a reference to my AsyncTask object to my new activity without implementing parcelable and adding it to the bundle which would be a huge mess I think. Is there a better way to do this?

我也看到其他人建议启动服务。什么是优势,做这种方式?谢谢!

I've also seen other people recommend starting a service. What would be the advantages to doing it this way? Thanks!

〜斯科特

推荐答案

这正是使用服务的情况。一个AsyncTask的是做事情的UI线程(一个方便的类真的)。

This is exactly the case for using a service. An AsyncTask is for doing things on a UI thread (a convenience class really).

你正在做应该在服务下去,因为你不必关心UI线程什么,因为你不会在同一个活动被显示。

What you are doing should go on in a service, because you don't necessarily care about the UI thread since you are not going to be displaying in the same activity.

下面是你应该如何去这样做:

Here is how you should go about doing this:

1)创建服务,做RSS提要的处理 2)的服务里的,你应该存储你的结果与ContentResolver的数据库 3)在你的ListActivity的ListAdapter监听同一CONTENT_URI为您服务正在更新的变化。这将自动更新您的列表作为行被添加或删除,没有任何ANRS,因为这一切都在后台线程

1) create a service and do the processing of the RSS feed 2) inside the service you should store your results in a database with the ContentResolver 3) Have your ListAdapter in your ListActivity listen for changes on the same CONTENT_URI as your service is updating. This will automatically update your list as the rows get added or deleted, without any ANRs since it's all on the background thread

其他的一些优点是,你可以在后台,而无需你的UI是开放的,在所有的同步。这可能是一个RSS的应用程序非常有用,因为你并不总是希望等待用户发起同步。

Some other advantages is that you can sync in the background without having your UI to be open at all. This could be very useful for a RSS application since you don't always want to wait for a user to initiate a sync.

这篇关于AsyncTask的在多个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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