0

My app has a chat screen to send and receive messages. Whenever a message is received a push notification comes in and shows the latest received message. This all works fine.

But when I get my push notification I would like the chat screen to refresh itself. I guess this would require knowing if the chat screen is currently visible or not. How can I do this from the onReceive() method of my BroadcastReceiver?

Here's a little pseudo code (in my BroadcastReceiver subclass):

public void onReceive(Context context, Intent intent) 
{
    if(currentlyVisibleActivity.getClass() == chatScreenActivity.class())
    {
        ((chatScreenActivity)currentlyVisibleActivity).getMessages();
    }

}

I would like to know how to get the currentlyVisibleActvity and make it call getMessages() if it's a chatScreenActivity.

PaulG
  • 6,920
  • 12
  • 54
  • 98
  • Are you storing the chat messages in a ContentProvider or similar? – Jens Apr 06 '12 at 19:55
  • When testing out the chat feature I pinged the server every 15 seconds to get new messages, then stored them on a local SQLite database. This was only for testing of the getMessages() method, I know that pinging the server every 15 seconds is not a good thing. So I'm looking to call getMessages() whenever I receive a push notification. – PaulG Apr 06 '12 at 19:58
  • Right-o. So implement a proper ContentProvider (check out [SQLiteContentProvider](http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android-apps/2.3_r1/com/android/apps/tag/provider/SQLiteContentProvider.java/?v=source) for a proper abstract implementation to extend). Whenever you call #getMessages() or similar just trigger the [notifyChange()](http://developer.android.com/reference/android/content/ContentResolver.html#notifyChange%28android.net.Uri,%20android.database.ContentObserver%29) method and any ListView (or similar) that is properly implemented will update. – Jens Apr 06 '12 at 20:02
  • I'm sorry I may have not worded the question properly (please see edits above). The getMessages() method works fine, I just want to know how to call it from the BroadcastReceiver's onReceive() method. I want to know how to check to see if the chat screen activity is visible or not. – PaulG Apr 06 '12 at 20:04
  • You typically *don't* do stuff like that in a receiver - you dispatch the work to an IntentService or similar that does the heavy lifting. – Jens Apr 06 '12 at 20:11

1 Answers1

1

I assume you are planning the following setup?

C2DM -> YourBroadcastReceiver -> somehow call #getMessages()

How about having the following components:

A BroadcastReceiver <for receiving C2DM push notifications>
An IntentService <for executing #getMessages()>
A ContentProvider <for storing the results of #getMessages()>
An Activity <for displaying the contents of ContentProvider>
  1. Basically, C2DM triggers your receiver which calls #startService, triggering your IntentService.
  2. Your IntentService calls #getMessages() and stores the messages in your ContentProvider.
  3. Whenever a change is performed on the data in the ContentProvider you will trigger ContentResolver#notifyChange().
  4. If your Activity is showing and uses, for instance, a CursorAdapter to read from the ContentProvider it will automatically refresh.

Sending a chat message would also store a row in the ContentProvider in this scenario - automatically updating the UI just as a received message.

Jens
  • 16,853
  • 4
  • 55
  • 52