Broadcast Receiver class and registerReceiver method

user2717079 picture user2717079 · Sep 17, 2013 · Viewed 72.2k times · Source

Hi i am trying to understand Broadcast Receiver , i went through many sample codes , but still have some doubts. I wanted to know when we have to extend the Broadcast Receiver class and when should we use registerReceiver() method and when should we create object for BroadcastReceiver. In some programs i came across registerReceiver methods being used but without extending the Broadcast Receiver class. I also wanted to know how the onReceive method gets called.

Which approach should be used when?

here is the registerReceiver method:

registerReceiver(new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                switch (getResultCode()) {
                ........
                }

            }

        }, new IntentFilter(SENT));

Object being created for BroadcastReceiver:

private BroadcastReceiver intentReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        .................
    }

};

Answer

Bhoomika Brahmbhatt picture Bhoomika Brahmbhatt · Sep 17, 2013

Android has intent action for broadcast receiver. BroadCast receiver will be trigger when it listen any action which registered within it.

Now we will take one example : That we need to listen the action of "whenever any bluetooth device connect to our device". For that android has it fix action android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED

So you can get it via manifest & registration also

BY Manifest Registration:

Put this in your manifest

<receiver android:name="MyBTReceiver">
    <intent-filter>
                <action android:name="android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED" />
      </intent-filter>
</receiver>

Create MyBTReceiver.class

public class MyBTReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().equals("android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED")){
            Log.d(TAG,"Bluetooth connect");
        }
    }
}

That was the simplest broadcast Receiver.

Now, if you are only interested in receiving a broadcast while you are running, it is better to use registerReceiver(). You can also register it within your existing class file. you also need to unregister it onDestroy(). here, you dont need any broadcast registration in manifest except activity registration

For example

public class MainActivity extends Activity {

    IntentFilter filter1;

    @Override
    public void onCreate() {
        filter1 = new IntentFilter("android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED");
        registerReceiver(myReceiver, filter1);
    }

    //The BroadcastReceiver that listens for bluetooth broadcasts
    private final BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equalsIgnoreCase("android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED")) {
                Log.d(TAG,"Bluetooth connect");
            }
        }
    };

    @Override
    public void onDestroy() {
        unregisterReceiver(myReceiver);
    }
}