how to check the real Internet connected in android?

江枫思渺然 提交于 2019-12-05 08:18:27
Frank

Just do a "Ping" to www.google.com chances that they are down are very low.

P.S. it's what we do in our app..

public static boolean isReachable(Context context) {
    //  First, check we have connectivity
    final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo netInfo = connMgr.getActiveNetworkInfo();

    if (netInfo != null && netInfo.isConnected()) {
        //  check if google is reachable
        try {
            URL url = new URL("http://www.google.com");
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(10 * 1000); // Ten seconds timeout in milliseconds
            urlc.connect();
            if (urlc.getResponseCode() == 200) { // success
                return true;
            } else { // Fail
                return false;
            }
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
            return false;
        }
    } else {
        return false;
    }
}

Please Refer below :

Make a method that return boolean value :

    ConnectivityManager connectivityManager;
    NetworkInfo wifiInfo, mobileInfo;

    public Boolean checkNow(Context con){
    try{
        connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
        wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);  

        if(wifiInfo.isConnected() || mobileInfo.isConnected())
        {
            return true;
        }
    }
    catch(Exception e){
        System.out.println("CheckConnectivity Exception: " + e.getMessage());
    }

    return false;
}   

Use the above method in your onCreate() Method Like Below :

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       boolean con = checkNow(getApplicationContext());

       if(con){
           Toast.makeText(getApplicationContext(), "Connection Founded", Toast.LENGTH_SHORT).show();
       }else{
           Toast.makeText(getApplicationContext(), "Connection Not Founded", Toast.LENGTH_SHORT).show();
       }

}

When you run the app you will prompted as "Connection Founded" if internet connection available in your device otherwise it will prompted "Connection Not Founded".

According to the below method, if device is not in airplane mode or no network available in any connectivity mode, method will return false, otherwise true. Bare in mind that if above scenarios are satisfied it will return null. Therefor it is handled by checking for null netInfo object.

public boolean isOnline() {
ConnectivityManager conManager =
    (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conManager .getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
    return true;
}
return false;

}

Remember to get permission to access network state to the manifest.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!