Dror Fichman Dror Fichman - 1 year ago 131
Android Question

Register to local broadcast inside a custom view

I've created a custom view which can be placed on different places in the application. I can't avoid using a BroadcastReceiver inside the view to get messages from the rest of the application.

I've read it's not recommended (Where should I unregisterReceiver in my own view?), but in case I choose to use it is there a place to unregister the view from the BroadcastManager?

Answer Source

I suggest you to use a LocalBroadcastManager. It's like a BroadcastReceiver whose Intents can only be seen inside your application.

private BroadcastReceiver receiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        // your code here

protected void onPause() {

protected void onResume() {
    LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
    IntentFilter filter = new IntentFilter();
    lbm.registerReceiver(receiver, filter);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download