Activity as Broadcast receiver

In this post I show how to instantiate a broadcast receiver in an activity.
An example for which this kind of broadcast receiver is useful is when calling the GCM library service to register/unregister from the GCM servers.

When the service asynchronously receives the response from GCM servers, if the activity where users configure the notifications is running in the foreground, it is necessary to update its view. The view shows the user if notifications were successful activated or not.

  • Create the broadcast receiver in the activity class.
    private BroadcastReceiver gcmReceiver = 
    new BroadcastReceiver() { 
      @Override
      public void onReceive(Context context, Intent intent) {
        // Update view
        updateView(intent.getStringExtra("com.belencruz.code"));
      }
    };
    
  • Call register and unregister methods in the activity class.
    public void onResume() {
      super.onResume();
      registerReceiver(gcmReceiver, 
                       new IntentFilter("com.belencruz.GCMResult"));  
    }
     
    public void onPause() {
      super.onPause();
      unregisterReceiver(gcmReceiver);  
    }
    
  • Broadcast from the service.
    public class GCMIntentService extends GCMBaseIntentService {
      ...
    
      @Override
      protected void onRegistered(Context context, String regId) {
        // ...
    
        Intent bcIntent = new Intent();
        bcIntent.putExtra("com.belencruz.code", GCMState.ACTIVE);
        bcIntent.setAction("com.belencruz.GCMResult");               
        context.sendBroadcast(bcIntent);
      }
    
      @Override
      protected void onUnregistered(Context context, String regId) {
        // ...
    
        Intent bcIntent = new Intent();
        bcIntent.putExtra("com.belencruz.code", GCMState.INACTIVE);
        bcIntent.setAction("com.belencruz.GCMResult");               
        context.sendBroadcast(bcIntent);
      }
    
      @override
      protected void onError(Context context, String errorId) {
        // ...
    
        Intent bcIntent = new Intent();
        bcIntent.putExtra("com.belencruz.code", GCMState.ERROR);
        bcIntent.setAction("com.belencruz.GCMResult");               
        context.sendBroadcast(bcIntent);
      }
    }
    
    Share Button

2 Comments

  1. Can the above code be used in any other activity to perform any task….
    E.g: if we have a GCMBroadcastReceiver class and there is another Activity too. What i want to do is that use the same functionality of GCMBroadcastReceiver within it private methods of that activity too. Can you please help me with that. Waiting for a reple as soon as possible, Thanks

    1. The code above can be used in any activity to receive any notification, and then to execute any task that is available for that activity. The example above is to communicate from the GCMBroadcastReceiver to any other activity. I am not sure if that is what you mean.

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.