Saturday, February 11, 2012

How to listen for Connectivity change in android?


This is a class I wrote for myself. It listens for connectivity changes and can launch callbacks when the internet connection goes up or down.
To use it, create a new instance in your onCreate, call bind(this) in onResume and unbind(this) in onPause.
You can then do two things: call hasConnection() at any time to check if there is an Internet connection or implementOnNetworkAvailableListener in order to get notified when the network state changes.
You'll need the following permissions:
  • android.permission.INTERNET
  • android.permission.ACCESS_NETWORK_STATE

public class ConnectivityReceiver extends BroadcastReceiver {
    
    public static interface OnNetworkAvailableListener {
        public void onNetworkAvailable();
        public void onNetworkUnavailable();
    }
    
    private final ConnectivityManager connectivityManager;
    private OnNetworkAvailableListener onNetworkAvailableListener;
    private boolean connection = false;
    
    public ConnectivityReceiver(Context context) {
        connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        checkConnectionOnDemand();
    }
    
    public void bind(Context context) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        context.registerReceiver(this, filter);
        checkConnectionOnDemand();
    }
    
    public void unbind(Context context) {
        context.unregisterReceiver(this);
    }

    private void checkConnectionOnDemand() {
        final NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info == null || info.getState() != State.CONNECTED) {
            if (connection == true) {
                connection = false;
                if (onNetworkAvailableListener != null) onNetworkAvailableListener.onNetworkUnavailable();
            }
        }
        else {
            if (connection == false) {
                connection = true;
                if (onNetworkAvailableListener != null) onNetworkAvailableListener.onNetworkAvailable();
            }
        }
    }
    
    @Override
    public void onReceive(Context context, Intent intent) {
        if (connection == true && intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) {
            connection = false;
            if (onNetworkAvailableListener != null) {
                onNetworkAvailableListener.onNetworkUnavailable();
            }
        }
        else if (connection == false && !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) {
            connection = true;
            if (onNetworkAvailableListener != null) {
                onNetworkAvailableListener.onNetworkAvailable();
            }
        }
    }
    
    public boolean hasConnection() {
        return connection;
    }
    
    public void setOnNetworkAvailableListener(OnNetworkAvailableListener listener) {
        this.onNetworkAvailableListener = listener;
    }

}

No comments:

Post a Comment