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;
    }
}

3
1
Upgrate 100 points

                                    public class CheckNetworkConnection extends AsyncTask < Void, Void, Boolean > {
    private OnConnectionCallback onConnectionCallback;
    private Context context;

    public CheckNetworkConnection(Context con, OnConnectionCallback onConnectionCallback) {
        super();
        this.onConnectionCallback = onConnectionCallback;
        this.context = con;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Boolean doInBackground(Void...params) {
        if (context == null)
            return false;

        boolean isConnected = new NetWorkInfoUtility().isNetWorkAvailableNow(context);
        return isConnected;
    }

    @Override
    protected void onPostExecute(Boolean b) {
        super.onPostExecute(b);

        if (b) {
            onConnectionCallback.onConnectionSuccess();
        } else {
            String msg = "No Internet Connection";
            if (context == null)
                msg = "Context is null";
            onConnectionCallback.onConnectionFail(msg);
        }

    }

    public interface OnConnectionCallback {
        void onConnectionSuccess();

        void onConnectionFail(String errorMsg);
    }
}

3 (1 Votes)
0
4
5
Jordano 95 points

                                    new CheckNetworkConnection(this, new CheckNetworkConnection.OnConnectionCallback() {

    @Override
    public void onConnectionSuccess() {
        Toast.makeText(context, "onSuccess()", toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionFail(String msg) {
        Toast.makeText(context, "onFail()", toast.LENGTH_SHORT).show();
    }
}).execute();

4 (5 Votes)
0
4
1
Darin 120 points

                                    class NetWorkInfoUtility {

    public boolean isWifiEnable() {
        return isWifiEnable;
    }

    public void setIsWifiEnable(boolean isWifiEnable) {
        this.isWifiEnable = isWifiEnable;
    }

    public boolean isMobileNetworkAvailable() {
        return isMobileNetworkAvailable;
    }

    public void setIsMobileNetworkAvailable(boolean isMobileNetworkAvailable) {
        this.isMobileNetworkAvailable = isMobileNetworkAvailable;
    }

    private boolean isWifiEnable = false;
    private boolean isMobileNetworkAvailable = false;

    public boolean isNetWorkAvailableNow(Context context) {
        boolean isNetworkAvailable = false;

        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        setIsWifiEnable(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected());
        setIsMobileNetworkAvailable(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected());

        if (isWifiEnable() || isMobileNetworkAvailable()) {
            /*Sometime wifi is connected but service provider never connected to internet
            so cross check one more time*/
            if (isOnline())
                isNetworkAvailable = true;
        }

        return isNetworkAvailable;
    }

    public boolean isOnline() {
        /*Just to check Time delay*/
        long t = Calendar.getInstance().getTimeInMillis();

        Runtime runtime = Runtime.getRuntime();
        try {
            /*Pinging to Google server*/
            Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
            int exitValue = ipProcess.waitFor();
            return (exitValue == 0);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            long t2 = Calendar.getInstance().getTimeInMillis();
            Log.i("NetWork check Time", (t2 - t) + "");
        }
        return false;
    }
}

4 (1 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
check internet conenxion android internet connection check android check internet android java internet check android Android check the internett android check internet check internet availability android android check internet usage checking for internet android internet connection check in android check if internet is available android how to check for internet connection in android internet availability checker android android check internet connection check internet available android check internet in android check if internet is on android android how to check internet connection best way to check internet connection android check internet connection android using network callbacks how to check for internet connection android check for internet connection android Check internet connection android check internet connection in android how to check internet in android android check if there is internet connection check internet permission android checking network availability using android studio how to check if mobile is kept connected to internet in android studio how to check for internet overflow check if device access to internet in android internet connectivity check android how to check network connection in android studio check internet before api call in android android kotlin NetworkConnectionManager android kotlinNetworkConnectionManager check for active internet connection android android internet connection check kop check ineternet connection in android Check to make sure it is "connected" to a internet in android best way for check internet connection in android android test if internet works android test if network works Android 9 Detect Internet Connection Status android check network available Internet check for android how to detect intenet is off in android app check status internet connection in android development check if wifi is enabled android 29 android check if device is connected to wifi android detect internet connection status no internet android example how to check network connection in android how to check internet connection in android webview check android internet connection android check for internet connection how to detect internet connection in android internet checked android check connection to internet android kotlin check if network connection android chk internet connection how to check wifi state in android programmatically android studio check internet connection code to is network connected in android check internet availability in android for both mobile data and wifi check internet availability in android get active network error android check if android is connected to wifi java check is an android device is connected to the internet android studio check if network connection CHECK INTENET LEVEL ANDROID network check android Listen To Internet Connection androdi p how to check if app is using wifi or mobile data in android studio how to check network connection in android example continuously check internet connection c# how to check for network connectivity in android android check internet connection with error check is enternet connected and available in service intent check internet connection android studio example if connextion android studio android check network connection check network state if condition with callback check netwtork state if condition how to check internet connection in android 10 set internet connectivity in android to check it works or not c# android check if network is available new way to know internet status in android which method can get device's wifi connection status in android programming android chek internet connection check connection android how to check internet acces in android detect netwokavailable andoid android network available check check where is app connected to android best network stat handle kotlin Internet checks in android how to check if my app connect internet How to check is internet on or off in Android studio how to check if mobile not connected with internet android studio android check if internet connection available check if have internet access android how to check internet connection fail android programatically when upload check if there internet connection in andriod check network connection android how to check mobile device has ingternet in android 10 android check internet in on how to check connection to internet android check if network is available android how to check for no internet in android android check if network exception android check connectivity check if device has internet connection android android studio check internet connection change java code for detecting server wifi check if app is connected to internet android connectivity checking in android Please verify that you have a stable internet connection kotlin Android Check Internet Connection java Internet not working validation in android android how to check stable internet connection are not app android detect if online check internet sconnection android android check if client is connected to internet detect network type in Android 10 and above? check if network is not avaiable in androidd android internet connectivity control other app internet access android stackoverflow android how to test connection internet connectivity check latest android check internet connectivity before api call in android check network in android application how to check android standroid studio not using internet connection android check connection check if your device has access to internet android android is network available check network connection android at app launch how to verify reconnection internet in android android internet connection check android isDeviceConnectedToInternet check internet connectivity android android show connections check internet connection in android and try again java how to check internet connection in android
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source