Answers for "android studio check internet connection"

1

android studio check network connection

//beware that (contextname) is your activity name like if your working on
//activity with name of FirstActivity then instead of (contextname)
//write **FirstActivity.this**.getSystemService...

ConnectivityManager cm =
        (ConnectivityManager)contextname.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
                      activeNetwork.isConnectedOrConnecting();

//in the end boolean Variable isConnected should hold the Status 
//of your network Connection if isConnected is true then you have active network
//otherwise if its false you have no internet Connection
Posted by: Guest on August-31-2021
2

Android check internet

/**
 * @author Pratik Butani
 */
public class InternetConnection {

    /**
     * CHECK WHETHER INTERNET CONNECTION IS AVAILABLE OR NOT
     */
    public static boolean checkConnection(Context context) {
        final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connMgr != null) {
            NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();

            if (activeNetworkInfo != null) { // connected to the internet
                // connected to the mobile provider's data plan
                if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                    // connected to wifi
                    return true;
                } else return activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
            }
        }
        return false;
    }
}
Posted by: Guest on April-27-2020

Code answers related to "android studio check internet connection"

Browse Popular Code Answers by Language