Pages

Wednesday, November 27, 2013

How to monitor network connectivity using BroadCastReceiver in Android

 1. Now a days most of applications uses INTERNET in their Application. So most of the times we need to know whether the INTERNET is available or not before making any network call..

2. INTERNET Connection might change anytime – and it probably will, given the volatility of a mobile environment.

3. Thankfully Android’s ConnectivityManager allows you to register for a broadcast event that is fired whenever this happens

4. The Source Code for the registering the Broadcast Receiver for finding out the  Network Connectivity is given below

public class Reachability {
    public boolean isReachable = false;
    public boolean isReceiving = false;
   
    private BroadcastReceiver receiver = null;
   
    public static Reachability reachability = null;
    static {
        reachability = new Reachability();
    }
   
    public static boolean registerReachability(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
       
        if (reachability.isReceiving) {
            context.unregisterReceiver(reachability.receiver);
            reachability.isReceiving = false;
        }
       
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        reachability.isReachable = info != null && info.isAvailable() && info.isConnected();
       
        reachability.receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                ConnectivityManager connectivityManager = (ConnectivityManager) context
                                .getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo info = connectivityManager.getActiveNetworkInfo();
                reachability.isReachable = info != null && info.isAvailable() && info.isConnected();
            }
           
        };
        context.registerReceiver(reachability.receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        reachability.isReceiving = true;
       
        return reachability.isReachable;
    }
}

5. In the next Step you need to register the BroadCast Receiver for finding out the  Network Connectivity in the Main Application class which extends Application as given below

public class ELSApplication extends Application {
    @Override
    public void onCreate() {

        Reachability.registerReachability(this.getApplicationContext());
      
    }
   
}

6. So whenever your are making the Network call , using the following code  you can check whether the INTERNET is available or not

 if (!Reachability.reachability.isReachable) {
                Toast.makeText(getActivity(), "No Internet", Toast.LENGTH_SHORT).show();
            } else {
                String[] url = { "https://www.dropbox.com" };
                DownloadFileAsync dloadFAsync = new DownloadFileAsync();
                dloadFAsync.execute(url);
            }

7. And the following permissions need to be added in the AndroidManifestFile

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

No comments:

Post a Comment