Search code examples
androidandroid-wifiandroid-networking

Keep checking if Device has internet connection


As title states I wanted to ask, how can I keep checking if the device has internet connection? I use the following class:

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;


public class checkConnection {

    private static checkConnection instance = new checkConnection();
    static Context context;
    ConnectivityManager connectivityManager;
    NetworkInfo wifiInfo, mobileInfo;
    boolean connected = false;

    public static checkConnection getInstance(Context ctx) {
        context = ctx.getApplicationContext();
        return instance;
    }

    public boolean isOnline() {
        try {
            connectivityManager = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        connected = networkInfo != null && networkInfo.isAvailable() &&
                networkInfo.isConnected();

        return connected;


        } catch (Exception e) {
            System.out.println("CheckConnectivity Exception: " + e.getMessage());
            Log.v("connectivity", e.toString());
        }
        return connected;
    }
}

But this only checks once the activity starts if the device has or not internet connection, if I wanted to keep checking how can I create a loop throughout the activity?


Solution

  • public class NetworkChangeReceiver extends BroadcastReceiver {
    
    @Override
    public void onReceive(final Context context, final Intent intent) {
    
     if(checkInternet(context))
     {
       Toast.makeText(context, "Network Available Do operations",Toast.LENGTH_LONG).show(); 
     }
    
    }
    
    
    boolean checkInternet(Context context) {
            ServiceManager serviceManager = new ServiceManager(context);
            if (serviceManager.isNetworkAvailable()) {
                return true;
            } else {
                return false;
            }
        }
    
    }
    

    ServiceManager.java

    public class ServiceManager extends ContextWrapper {
    
    public ServiceManager(Context base) {
        super(base);
    }
    
    public boolean isNetworkAvailable() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        }
        return false;
    }
    
    }
    

    permissions :

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

    And don't forget to register the receiver like this in main activity -

    registerReceiver(mConnReceiver, 
               new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
       }
    }
    

    ref. here