Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call onResume() or onCreate() from GCMIntentService

Tags:

android

I am developing application that is using GCMIntentService for Push Notifications. So I want to refresh the Data of one ListView that is in a tab of my TabActivity. So is it possible to do that from the GCMIntentService when i receive and GCMIntentMessage?

For example

SecondTabActivity.callOnResume(); //or something like this

or

SecondTabActivity.callOnCreate(); // or something like this

I need to do this without using startActivity(intent); cause If I do the SecondTabActivity is getting out of my TabHost and it's starting like new Activity. The function for refreshing the ListView is located at onCreate() and onResume() in the SecondTabActivity so that is why I want to call them. If there is any other way beside this one please refer to it. Thanks

like image 415
Naskov Avatar asked Dec 19 '12 09:12

Naskov


2 Answers

A good way is to use broadcast intents.

  1. In your TabActivity's onCreate, use the Context.registerReceiver method to register a broadcast receiver for an intent named something like "MyGCMMessageReceived."

  2. In that broadcast receiver, call the method that refreshes your list.

    this.registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // INSERT CODE TO REFRESH LIST VIEW
        }
    }, new IntentFilter("MyGCMMessageReceived"));
    
  3. Make your GCMIntentService's onMessage method broadcast an intent named "MyGCMMessageReceived".

        Intent intent = new Intent("MyGCMMessageReceived");
        this.sendBroadcast(intent);
    
like image 184
tom Avatar answered Sep 19 '22 22:09

tom


You should use LocalBroadcastManager in Service to Activity Communications.

In your IntentService you send the new information via Local broadcast and in your TabActivity your have to register a receiver. In onReceive method you have to refresh your view.

Here you have an easy example: LocalBroadcastManager example

like image 35
frayab Avatar answered Sep 19 '22 22:09

frayab