0

I want to declare Broadcast Receiver to all my activities of the app for detect when the conexion of internet goes. As is done? Thank you

Manifest

 <receiver
            android:name=".managers.ConnectionReceiver"
            android:label="NetworkChangeReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>

Class extends BroadcastReceiver

public class ConnectionReceiver extends BroadcastReceiver {

    ConnectionListener connectionListener = new MainActivity();
    ConnectivityManager connectivityManager;
    NetworkInfo activeNetInfo;

    public ConnectionReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
    connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    activeNetInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetInfo != null) {
        connectionListener.onConnect();
    }else{
        connectionListener.onDisconnect();
    }
}
Eric Retamero
  • 51
  • 1
  • 5
  • Possible duplicate of [Programmatically register a broadcast receiver](http://stackoverflow.com/questions/4805269/programmatically-register-a-broadcast-receiver) – Beloo Oct 13 '16 at 08:08

3 Answers3

1

The simplest way would be to create a parent class for all of your activities that define this behavior and subclass from there.

for example...

class ConnectionListeningActivity extends Activity {

    protected boolean isConnected;

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
         void onReceive(Context context, Intent intent) {
              updateNetworkState();
         }
    };

    public void updateNetworkState() {
         ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

         NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
         isConnected = activeNetwork != null &&
                  activeNetwork.isConnectedOrConnecting();
    }

    public void onResume() {
        registerReceiver(broadcastReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
        updateNetworkState();
    }

    public void onPause() {
        unregisterReceiver(broadcastReceiver);
    }

}

Here we've registered the broadcastReceiver in onResume method and unregistered it in onPause so its not active when the activity isn't.

We're also updating the isConnected status in onResume, because we may or may not receive a networkStateChange before the isConnected is first used..

now we can create a subclass that uses the isConnected field to perform some action..

Pasi Matalamäki
  • 1,843
  • 17
  • 14
0

In all the activities, create an instance of the broadcast receiver and register the broadcast receiver.

ConnectionReceiver receiver = new ConnectionReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("ACTION HERE");
getApplicationContext().registerReceiver(receiver, filter)

NOTE: Unregister receiver onStop() method since the onPause() method is called in Android 7 during multitasking and register receiver again in onStart() instead of onResume()

Geet Choubey
  • 1,069
  • 7
  • 23
0

Add below lines in Activities you want

  ConnectionReceiver receiver = new ConnectionReceiver();

    public void onResume(){
    super.onResume();
    registerReceiver(receiver ,new IntentFilter("action_to_perform"));
    }

    public void onPause(){
    super.onPause();
    unregisterreceiver(receiver);
    }
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37