Search code examples
androidbroadcastreceiverintentfilter

Android registerReceiver with Intent filter: Do I need to check the action?


Let's say I have this receiver:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(ACTION.equals(intent.action)){
                doSth()
            }      
        };

I then register it dynamically like that:

mContext.registerReceiver(mReceiver, new IntentFilter(ACTION));

Do I need to check inside the receiver with

 if(ACTION.equals(intent.action)){
                doSth()
            }

since inside the method registerReceiver I put an intentFilter with ACTION?


Solution

  • You do not need to check for action assuming your filter is correct. As per documentation, "The receiver will be called with any broadcast Intent that matches filter, in the main application thread". More info: https://developer.android.com/reference/android/content/Context.html#registerReceiver(android.content.BroadcastReceiver,%20android.content.IntentFilter)