简体   繁体   中英

how to check the real Internet connected in android?

I make a program, and I have to use wifi to connect Internet. I find some information to check whether the wifi is connected or not. But in some situation, you can connect the wifi AP but you still can't use Internet like the wifi needed account and password to certificate in https, or the wifi AP is not able to Internet. So, how can I check the real Internet connected?

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

PS 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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM