Determine and Monitoring Connectivity Status

Today most of the android application depends on the Internet connectivity for regular updates of application data from Internet resources or to execute long running downloads.

In this article I will try to provide a solution, which constantly monitors connectivity changes in the application and let developer pleasantly handle those changes.

In order to understand this article I hope that you are little bit familiar with Android BroadcastReceiver.

I have created a ConnectivityBroadCast java class, which constantly monitors the Connectivity status in our application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class ConnectivityBroadCast extends BroadcastReceiver {
 
    private boolean isNetworkAvailble;
    private List networkMonitoringListeners;
 
    ConnectivityBroadCast(Context context) {
        networkMonitoringListeners = new ArrayList();
        getNetworkState(context);
    }
 
    @Override
    public void onReceive(Context context, Intent intent) {
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            if (getNetworkState(context))
                notifyAllNetworkListener();
        }
    }
 
    private void notifyAllNetworkListener() {
        for (NetworkMonitoringListener listener : networkMonitoringListeners) {
            notifyNetworkListener(listener);
        }
    }
 
    private void notifyNetworkListener(NetworkMonitoringListener listener) {
        if (listener != null)
            if (isNetworkAvailble)
                listener.networkAvailable();
            else
                listener.networkUnavailable();
    }
 
    private boolean getNetworkState(Context context) {
        boolean prevNetworkState = isNetworkAvailble;
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        isNetworkAvailble = networkInfo != null && networkInfo.isConnected();
        return prevNetworkState != isNetworkAvailble;
    }
 
    public void registerNetworkMonitoringListener(NetworkMonitoringListener listener) {
        networkMonitoringListeners.add(listener);
        notifyNetworkListener(listener);
    }
 
    public void unregisterNetworkMonitoringListener(NetworkMonitoringListener listener) {
        networkMonitoringListeners.remove(listener);
    }
 
 
    public interface NetworkMonitoringListener {
        void networkUnavailable();
 
        void networkAvailable();
    }
}

How to use this class: –

  • You have to register this receiver at onResume Api of the activity.
  • You have to unregister this receiver at onPause Api of the activity.

ConnectivityBroadCast provides you: –

  • Two callback functions
    • networkAvailable()
    • networkUnAvailable()
  • At the start of the activity it tells you if connectivity is available or not.
  • It also monitors the connectivity status while interacting with the activity, and gives you a callback if there is any change in the connectivity status.

Use this class in your code, and you are done handling with uncertain network connections.

If you want to know, how to implement and use this class.  I have put the complete code on Github here.

References : – 

Leave a Reply

Your email address will not be published.