Search code examples
javaandroidandroid-networking

Background thread to detect Network is available


I'm developing app that has to detect automatically when the network is available . I'm thinking of running background thread on timer basis to detect when the Network is available, is this Correct method or is there any standard procedure to check it ?


Solution

  •     you can use android.net.conn.CONNECTIVITY_CHANGE broadcast
    
        Add in your manifest
    
    
    
         <receiver android:name="com.example.utils.CheckInternetConnection"
            android:enabled="true" android:label="ConnectivityActionReceiver">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
            </intent-filter>
        </receiver>
    
    
    
        public class CheckInternetConnection extends BroadcastReceiver {
    
    
            @Override
            public void onReceive(final Context arg0, Intent arg1) {
    
        if (haveNetworkConnection(arg0)) {
        //TODO iF INTERNET IS CONNECTED 
        }else{
        //TODO iF INTERNET IS DISCONNECTED 
    
    
        }
    }
            private boolean haveNetworkConnection() {
                boolean haveConnectedWifi = false;
                boolean haveConnectedMobile = false;
    
                try {
                    android.net.ConnectivityManager cm = (android.net.ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
                    android.net.NetworkInfo[] netInfo = cm.getAllNetworkInfo();
                    for (android.net.NetworkInfo ni : netInfo) {
                        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                            if (ni.isConnected())
                                haveConnectedWifi = true;
                        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                            if (ni.isConnected())
                                haveConnectedMobile = true;
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return haveConnectedWifi || haveConnectedMobile;
            }
    }