views:

33

answers:

1

My application has an IntentService which runs periodically to download data from a remote server. The download task is run inside an AsyncTask which has reference to Application context only.

The data downloaded is for entire application and is displayed across four different activities. When the download completes(AsyncTask completes) the service is notified of the same. Now, the service needs to notify appropriate activity to update its view. The service is not aware of which Activity of what application is in foreground.

How do I make that connection? How does the service pass a message to the activity in foreground that new data is available and it should update its views?

Please help.

Thanks.

+1  A: 

One way is to send a broadcast, and if you have different types of activities the one that is active will receive it.

Pentium10
I don't think activities can receive Intents sent via context.sendBroadcast(). If the service broadcasts an intent, I will have to write a Broadcast receiver to capture the intent. How will the Broadcast receiver identify which of the Activity is in foreground? I cannot startActivities since user will be taken to new screens which is unexpected. Please correct me if I am wrong here.
Samuh
Activities can receive Broadcast if you implement an anonymous broadcast receiver in your Activity.
Pentium10
got confused first, when I read your response.:)Thanks!
Samuh