2

I've follow this code: How to get RegistrationID using GCM in android

but i want update the TextView of MainActivity with received message. please help me why i don't know how i update a UI from service

Community
  • 1
  • 1
Antonio
  • 1,181
  • 4
  • 15
  • 34

1 Answers1

6

I suggest to use Broadcast receivers here. So IntentService will broadcast the event, and all activities or something else can register for it.

To avoid overkill of BroadcastReceivers itself (as they are intented to be used in inter-process communication and can be an overkill and security leak) - I reccoment using LocalBroadcastManager - http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html . It's a helper for strictly-local broadcasts, makes easier to handle them and provide more performance and security.

Update:

To use LocalBroadcastReceiver inside GCM IntentService you should do something like this:

@Override
protected void onMessage(Context context, Intent intent) {
    Intent bIntent = new Intent("custom-event-name");
    // You can also include some extra data.
    bIntent .putExtra("message", intent); //or get some info from Gcm intent and put it into this one.
    LocalBroadcastManager.getInstance(context).sendBroadcast(bIntent);
} 

And here you can check how to register the receiver inside your activity, it's pretty easy: how to use LocalBroadcastManager?

Good luck

Community
  • 1
  • 1
AlexN
  • 2,544
  • 16
  • 13
  • thanks alexn, i'm newbie of android, please apply the modify to code and show me... sorry for my english. – Antonio Sep 03 '12 at 21:28
  • Actually, there is even no need to paste a code - the topic is described here with details: http://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager Hope it helps – AlexN Sep 03 '12 at 21:34
  • but i don't know ho to apply the code why the received extends GCMBaseIntentService.... help me please... – Antonio Sep 03 '12 at 21:41
  • I'd modified the answer, please check it. – AlexN Sep 03 '12 at 21:49
  • one moment... but where do i put the code for edit the textview? – Antonio Sep 03 '12 at 21:56