tony9099
tony9099

Reputation: 4727

programmatic vs static broadcast receivers in Android

In my app, I am utilizing GCM and push notifications. However, I am a bit concerned regarding broadcast receivers. In the app, I see the broadcast receivers are being declared 2 types.

  1. static via the manifest

       <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.myapp" />
        </intent-filter>
    </receiver>
    
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
  2. programmatic inside activity

       private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
    
        String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
    
        WakeLocker.acquire(getApplicationContext());
    
        Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
    
        WakeLocker.release();
    }  };
    

My questions are as follows:

QUESTION 1: Can we remove the programmatic declaration of broadcast receiver and still get a totally valid notification ? if yes, is the reason because we have it in the manifest already ?

QUESTION 2: If the receiver is declared both static and programmatic, which one is called when ? Which one has the priority ? If the app is open and running the programmatic one gets called ? and in case the app is not running, the static one ?

Upvotes: 1

Views: 716

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007296

Question #1 makes no sense to me, sorry.

If the receiver is declared both static and programmatic, which one is called when ?

If they both have IntentFilters that match the same broadcast, they both will receive the broadcast. For regular broadcasts, the order in which they will receive the broadcast is indeterminate.

Which one has the priority ?

For regular broadcasts, neither have "the priority" -- both get the broadcast, and you have no way of knowing who gets it first (and on multi-core devices, they could get the broadcast at the same time, in theory). Ordered broadcasts allow you to control priority of delivery, and only one of your receivers will get the ordered broadcast at a time.

If the app is open and running the programmatic one gets called ?

If "the app is open and running" means that you have your receiver registered via registerReceiver() and the manifest, then they both receive the broadcast.

and in case the app is not running, the static one ?

If "not running" means that you have unregistered your receiver that you registered via registerReceiver(), then only the receiver registered in the manifest will receive the broadcast.

Upvotes: 2

Related Questions