3

I have implemented GCM in my android application and it's working fine with receiving messages. The BroadcastReceiver is set up in the manifest-file according to the examples provided by Google.

My question is the following: If the user is having the application open and I want to update some results in that view - how can this be done? I was first thinking of registering this activity as a listener on whatever the BroadCastReceiver recieves. However, this must then be a static list of listeners, as a new instance of the BroadcastReceiver will be set up - but maybe this is not the way to do this.

This is what I currently have

 public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
 ComponentName comp = new ComponentName(context.getPackageName(),
 GCMIntentService.class.getName());
 startWakefulService(context, (intent.setComponent(comp)));
 setResultCode(Activity.RESULT_OK);
 }
 }
 public class GCMIntentService extends IntentService {
 public static final int NOTIFICATION_ID = 1;
 private NotificationManager mNotificationManager;
 NotificationCompat.Builder builder;
 public GCMIntentService() {
 super("GCMIntentService");
 }
 @Override
 protected void onHandleIntent(Intent intent) {
 Bundle extras = intent.getExtras();
 GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
 String messageType = gcm.getMessageType(intent);
 if (!extras.isEmpty()) {
 if (GoogleCloudMessaging.
 MESSAGE_TYPE_MESSAGE.equals(messageType)) {
 /** How to check if the activity 
GameActivity is running, and hence send an 
update signal to it? If it's not running a 
notification should be created.
 **/
 }
 }
 GCMBroadcastReceiver.completeWakefulIntent(intent);
 }
 }

Here's the important part for this in the manifest-file:

 <receiver
 android:name="q.w.e.gcm.GCMBroadcastReceiver"
 android:permission="com.google.android.c2dm.permission.SEND" >
 <intent-filter>
 <action android:name="com.google.android.c2dm.intent.RECEIVE" />
 <category android:name="q.w" />
 </intent-filter>
 </receiver>
 <service android:name="q.w.e.gcm.GCMIntentService" />

Any advice?

Thanks!

asked Aug 19, 2013 at 10:24

1 Answer 1

3

There are two ways to handle this.

1. Check if activity is running.

ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if(componentInfo.getPackageName().equalsIgnoreCase("com.yourpackagename")){
 //Activity Running
 Send a broadcast with the intent-filter which you register in your activity
 where you want to have the updates
} 
else{
 //Activity Not Running
 //Generate Notification
}

2. Using SendOrderedBroadcast.

This blog will give you an idea on how this could be achieved.

answered Aug 19, 2013 at 10:41
Sign up to request clarification or add additional context in comments.

2 Comments

I just updated my question with some minimized code for how it looks like. Let's say I have an activity called GameActivity. With your code above, if this activity is running, i would know the name of it. But how exactly do I broadcast this to the running instance?
You register a broadcast receiver in onResume of GameActivity and un-register in OnPause, Then you just have to broadcast intent using sendBroadcast method. Your activity will be able to catch the broadcast if it is running. :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.