Skip to content
Advertisement

Check internet status from the main activity

I’m new student on android development, so I don’t have the enough experience for coding, so I need the help from you… I create a java class on android studio to check if there is an internet connection or not :

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;


public class InternetStatus {
private static final String LOG_TAG ="InternetStatus";
public static boolean hasActiveInternetConnection(Context context) {
    if (isNetworkAvailable((Activity) context)) {
        try {
            HttpURLConnection urlc = (HttpURLConnection) (new     URL("http://www.google.com").openConnection());
            urlc.setRequestProperty("User-Agent", "Test");
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(1500);
            urlc.connect();
            return (urlc.getResponseCode() == 200);
        } catch (IOException e) {
            Log.e(LOG_TAG, "Error checking internet connection", e);
        }
    } else {
        Log.d(LOG_TAG, "No network available!");
    }
    return false;
}
public static boolean isNetworkAvailable(Activity mActivity) {
    Context context = mActivity.getApplicationContext();
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        return false;
    } else {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}


}

So, I have two questions :

1- is this the right code or I’m missing something ?

2- as you see this is a java class, if I create a button on my main activity to check if there is an internet connection or not !! how I can do that by importing this class or something like that ?!!

Advertisement

Answer

// Check if there is any connectivity for a Wifi network
public boolean isConnectedWifi(){
    NetworkInfo info = Connectivity.getNetworkInfo(Context);
    return info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI;
}

// Check if there is any connectivity for a mobile network
public boolean isConnectedMobile(){
    NetworkInfo info = Connectivity.getNetworkInfo(Context);
    return info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE;
}

// Check all connectivities whether available or not
public boolean isNetworkAvailable() {
    ConnectivityManager cm = (ConnectivityManager) 
      getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isConnected();
}

How to use?

For example, we need to check if the connectivity is available:

if(isNetworkAvailable()){
    // do your thing here.
}

When the network is available, we need to check whether it is WiFi or Mobile network:

if(isConnectedWifi()){
    // do your thing here with WiFi Network.
}

if(isConnectedMobile()){
    // do your thing here with Mobile Network.
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement