1

我有两个活动。我有一个活动,我需要获取 facebook 朋友数据、他们的事件,并将所有这些数据保存到数据库中,然后显示一个包含特定信息的页面,但这一切都需要很多时间。所以在做这一切之前,我将一个意图转移到另一个活动中,所以活动#1 在后台创建解析器并将其保存在数据库中。但是现在我需要在所有计算完成后通知我的 Activity #2,以便我也可以启用一些 UI 按钮来显示现在存储在数据库中的数据。

4

1 回答 1

0

If you really need both of those Activities (because you don't need Activity#1 if it's only task is to download data), then AsyncTask will be no good, since it's designed to be attached to a specific Activity.

What you'd rather do is to start a Service (with AsyncTask in it or even better, an IntentService). When it finishes its job you could do something like this:

Intent finish = new Intent(com.mypackage.intent.ACTION_DOWNLOADCOMPLETE);
sendBroadcast(finish);

And in your Activity#2 listen to this broadcast with BroadcastReceiver.

于 2012-09-21T07:55:10.030 回答