Android - Register/Unregister Receiver Dynamically using a Helper Class with Static Methods?

235 views Asked by At

THE SITUATION:
I have a 'helper' class with static methods, to dynamically enable/disable a BroadcastReceiver.
I call these methods from other classes, when I need to register (or) unregister the Receiver.

THE ISSUE:
I can't figure out how to dynamically UNRegister the receiver from within the helper class..
(When I try to do context.unregisterReceiver(myReceiver); it doesn't recognize myReceiver).

THE QUESTION:
How do I properly reference myReceiver from the receiverUnregister method in the Code below?


public class GpsReceiverHelper {


    //  Called from a different Class when I need to Register the Receiver
    public static void receiverRegister(Context context) {

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.location.PROVIDERS_CHANGED");

        final BroadcastReceiver myReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                //  Receiver code goes here    
            }
        };
        context.registerReceiver(myReceiver, intentFilter);
    }



    //  Called from a different Class when I need to UN-Register the Receiver
    public static void receiverUnregister(Context context) {

        //  THIS DOESN'T WORK BECAUSE "myReceiver" can't be found..
        context.unregisterReceiver(myReceiver);

    }


}


Again, I plan to call these Methods from within a different class.
What is the proper way to go about doing this, and properly assign everything?

3

There are 3 answers

0
Studio2bDesigns On BEST ANSWER

I managed to solve my issue by adding Field entries for static BroadcastReceiver myReceiver and then assigning it like myReceiver= ... inside of my static method containing the actual BroadcastReceiver code.. Furthermore, I realized I could avoid the BroadcastReceiver being killed upon calling finish(); simply by replacing context.registerReceiver with context.getApplicationContext.registerReceiver. Problem(s) solved!

1
takecare On

You'd have to keep references to all the receivers you register. You can do this in different ways. One way is to have a static collection that holds them in this helper class (not the best approach). Another way is to return the receiver in your receiverRegister method. This way whoever is calling the method has to maintain the reference and later on use it to unregister.

public BroadcastReceiver registerReceiver(...) {
  ...
  return receiver;
}

public void unregisterReceiver(Context context, BroadcastReceiver receiver) {
  context.unregisterReceiver(receiver);
}
0
Enzokie On

You can encapsulate the process by making intentFilter as member of GpsReceiverHelper class and basically removing the static keyword from recieverRegister and receiverUnregister.

public class GpsReceiverHelper {

    private final BroadcastReceiver myReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                //  Receiver code goes here    
            }
    };

    //  Called from a different Class when I need to Register the Receiver
    public void receiverRegister(Context context) {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.location.PROVIDERS_CHANGED");
        context.registerReceiver(myReceiver, intentFilter);
    }

    //  Called from a different Class when I need to UN-Register the Receiver
    public void receiverUnregister(Context context) {
        context.unregisterReceiver(myReceiver);
    }
}

The code above is now a real Helper class rather than just a utility class with a bunch of static methods.

Example usage:

class TestActivity extends Activity{
  private GpsRecieverHelper helper = new GpsRecieverHelper();

  public void onStart(){
      helper.receiverRegister(this);
  }

  public void onStop(){
      helper.receiverUnregister(this);
  }
}

If you need to reuse the same instance of the helper you can use public static keyword:

public static GpsRecieverHelper helper = new GpsRecieverHelper();

The helper above is now accessible in every class e.g. <NameOfTheCLass>.helper.

Disclaimer : I never tested the code myself in an actual IDE.