android communicate between activity and broadcast receiver

I think building the receiver as a private subclass of your activity is the way to go here. This way you can control events and data from your activity. Then you can just create one instance of it and register the receiver from code as you did above.

Note that you don't have to register your receiver in both the manifest and code. One of these is enugh - the manifest is basically a "static" registration while doing it in code allows dynamic registration at runtime. Also when you register in the manifest, a new instance of your receiver will automatically be created from the system, executed and terminated. Doing the reg in code allows to point to one specific instance.


Interface Approach!

You can communicate via an interface as well. This approach works even if your BroadcastReceiver is in a seperate file. You will not even have to access UI elements of Activity in the Broadcast.

Its pretty Straightforward. Just follow these 3 simple steps.

1) Create an interface

public interface MyListerner{

    public void performSomething(String arg);

} 

2) Initialize the Listener in ConnectionChangeReceiver

public class ConnectionChangeReceiver extends BroadcastReceiver {

      private MyListerner listener;

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

            listener = (MyListerner )context;  // initialse

            ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
            if (activeNetInfo != null) {

              listener.performSomething("Some data");   // Call listener method
              
            } 
      }
}

3) Implement the interface and Override the method in your Activity

public class YourActivity implements MyListerner{
     
      // Activity relate stuff onCreate() etc 

     public void updateUI(String result){
         // Your code to update UI
     }

     @Override
     public void performSomething(String arg){
           updateUI(arg);
     }

}

Relevant Links: You can read in detail Why using an interface is a preferred approach in this case