android check internet

Solutions on MaxInterview for android check internet by the best coders in the world

showing results for - "android check internet"
Simone
06 Mar 2018
1/**
2 * @author Pratik Butani
3 */
4public class InternetConnection {
5
6    /**
7     * CHECK WHETHER INTERNET CONNECTION IS AVAILABLE OR NOT
8     */
9    public static boolean checkConnection(Context context) {
10        final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
11
12        if (connMgr != null) {
13            NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();
14
15            if (activeNetworkInfo != null) { // connected to the internet
16                // connected to the mobile provider's data plan
17                if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
18                    // connected to wifi
19                    return true;
20                } else return activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
21            }
22        }
23        return false;
24    }
25}
Mariangel
05 Jan 2020
1class NetWorkInfoUtility {
2
3    public boolean isWifiEnable() {
4        return isWifiEnable;
5    }
6
7    public void setIsWifiEnable(boolean isWifiEnable) {
8        this.isWifiEnable = isWifiEnable;
9    }
10
11    public boolean isMobileNetworkAvailable() {
12        return isMobileNetworkAvailable;
13    }
14
15    public void setIsMobileNetworkAvailable(boolean isMobileNetworkAvailable) {
16        this.isMobileNetworkAvailable = isMobileNetworkAvailable;
17    }
18
19    private boolean isWifiEnable = false;
20    private boolean isMobileNetworkAvailable = false;
21
22    public boolean isNetWorkAvailableNow(Context context) {
23        boolean isNetworkAvailable = false;
24
25        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
26
27        setIsWifiEnable(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected());
28        setIsMobileNetworkAvailable(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected());
29
30        if (isWifiEnable() || isMobileNetworkAvailable()) {
31            /*Sometime wifi is connected but service provider never connected to internet
32            so cross check one more time*/
33            if (isOnline())
34                isNetworkAvailable = true;
35        }
36
37        return isNetworkAvailable;
38    }
39
40    public boolean isOnline() {
41        /*Just to check Time delay*/
42        long t = Calendar.getInstance().getTimeInMillis();
43
44        Runtime runtime = Runtime.getRuntime();
45        try {
46            /*Pinging to Google server*/
47            Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
48            int exitValue = ipProcess.waitFor();
49            return (exitValue == 0);
50        } catch (IOException e) {
51            e.printStackTrace();
52        } catch (InterruptedException e) {
53            e.printStackTrace();
54        } finally {
55            long t2 = Calendar.getInstance().getTimeInMillis();
56            Log.i("NetWork check Time", (t2 - t) + "");
57        }
58        return false;
59    }
60}
Finn
15 Oct 2016
1new CheckNetworkConnection(this, new CheckNetworkConnection.OnConnectionCallback() {
2
3    @Override
4    public void onConnectionSuccess() {
5        Toast.makeText(context, "onSuccess()", toast.LENGTH_SHORT).show();
6    }
7
8    @Override
9    public void onConnectionFail(String msg) {
10        Toast.makeText(context, "onFail()", toast.LENGTH_SHORT).show();
11    }
12}).execute();
Mélissa
25 Aug 2016
1public class CheckNetworkConnection extends AsyncTask < Void, Void, Boolean > {
2    private OnConnectionCallback onConnectionCallback;
3    private Context context;
4
5    public CheckNetworkConnection(Context con, OnConnectionCallback onConnectionCallback) {
6        super();
7        this.onConnectionCallback = onConnectionCallback;
8        this.context = con;
9    }
10
11    @Override
12    protected void onPreExecute() {
13        super.onPreExecute();
14    }
15
16    @Override
17    protected Boolean doInBackground(Void...params) {
18        if (context == null)
19            return false;
20
21        boolean isConnected = new NetWorkInfoUtility().isNetWorkAvailableNow(context);
22        return isConnected;
23    }
24
25    @Override
26    protected void onPostExecute(Boolean b) {
27        super.onPostExecute(b);
28
29        if (b) {
30            onConnectionCallback.onConnectionSuccess();
31        } else {
32            String msg = "No Internet Connection";
33            if (context == null)
34                msg = "Context is null";
35            onConnectionCallback.onConnectionFail(msg);
36        }
37
38    }
39
40    public interface OnConnectionCallback {
41        void onConnectionSuccess();
42
43        void onConnectionFail(String errorMsg);
44    }
45}
queries leading to this page
please verify that you have a stable internet connection kotlinandroid check internet connectionhow to check for internet connection in androidhow to check for no internet in androidinternet connectivity check androidno internet android examplecheck is an android device is connected to the internetbest way to check internet connection android check if network is available androidandroid check internet usageandroid check if there is internet connectioncheck internet in java androidhow to check internet in androidcheck if have internet access androidbest way for check internet connection in androidhow to check internet connection in android webviewandroid check internet accessinternet not working validation in androidcheck for internet androidandroid 9 detect internet connection statuscheck status internet connection in android developmentcheck netwtork state if conditionandroid check internet in onhow to check if my app connect internetandroid network available checkinternet checks in androidget active network error androidwhich method can get device 27s wifi connection status in android programmingcheck internet android javaandroid internet connection checkandroid is network availablecheck internet connection androidhow to check internet connection in androidhow to check network connection in android exampleandroid check internet connection javaandroid kotlin networkconnectionmanagercheck connection to internet androidandroid show connectionsandroid test if network workshow to check internet acces in androidcheck internet availability androidandroid how to check internet connectioncheck network connection android check where is app connected to androidcheck if app is connected to internet androidandroid chk internet connectioninternet connection check androidcheck to make sure it is 22connected 22 to a internet in androidcheck internet before api call in androidinternet connectivity check latest androidhow to check network connection in androidhow to check for internet connection androidhow to check for internet overflowcheck internet availability in androidhow to detect intenet is off in android appcheck android internet connectioncode to is network connected in androidcheck internet connection android studio exampleandroid studio check internet connectioncheck connection androidandroid check if network exceptionjava code for detecting server wifiapp android detect if onlineandroid check connectionandroid check if internet connection availablenetwork check androidinternet connection check in android examplecheck network connection android at app launchcheck is enternet connected and available in service intentchecking network availability using android studiocheck if there internet connection in andriodandroid check if device is connected to wifiinternet access check androidandroid check if internet is availablecheck intenet level androidandroid check network availablehow to check connection to internet androidandroid check the internettcheck if your device has access to internet androidandroid how to test connectioncheck if wifi is enabled android 29check if device access to internet in androidhow to check if mobile is kept connected to internet in android studiocheck internet in androidandroid studio check internet connection changehow to check if android has internetlisten to internet connection androdi pconnectivity checking in androidinternet connection check in androidcheck ineternet connection in androidandroid chek internet connectionandroid isdeviceconnectedtointernetchecking for internet androidc 23 android check if network is availablecheck network state if condition with callbackinternet check androidcheck internet connection android using network callbackscheck internet android studioif connextion android studiobest network stat handle kotlinandroid studio check if network connectioncontrol other app internet access android stackoverflowcheck internet sconnection androidandroid how to check stable internet connection are notcheck internet connectivity androidcheck internet conenxion androidinternet check for androidandroid check if client is connected to internetandroid check internetcheck if internet is available androidhow to check is internet on or off in android studiocheck if network is not avaiable in androiddset internet connectivity in android to check it works or nothow to check android standroid studio not using internet connectionandroid internet connectivitycheck internet connection in androidandroid check connectivityhow to check network connection in android studiocheck internet availability in android for both mobile data and wifiandroid kotlinnetworkconnectionmanagercheck for active internet connection androidcheck network in android applicationhow to check if mobile not connected with internet android studionew way to know internet status in android android detect internet connection statuscontinuously check internet connection c 23how to check internet connection fail android programatically when uploadhow to check internet connection in android 10android check for internet connectionhow to check mobile device has ingternet in android 10check internet permission androidandroid test if internet worksandroid check internet connection with errorhow to check if app is using wifi or mobile data in android studioandroid internet connection check kophow to check for network connectivity in androidinternet checked androidhow to detect internet connection in androidandroid check network connection how to check wifi state in android programmaticallycheck internet connection in android and try again javaknow if internet androidkotlin check if network connectioncheck if android is connected to wifi javacheck for internet connection androidcheck if internet is on androiddetect network type in android 10 and above 3fdetect netwokavailable andoidcheck if device has internet connection androidinternet availability checker androidhow to check if internet is available in androidcheck internet available androidhow to verify reconnection internet in androidcheck internet connectivity before api call in androidandroid check internet