Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check the real Internet connected in android?

Tags:

android

wifi

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?

like image 333
aaa SA Avatar asked Nov 04 '12 10:11

aaa SA


3 Answers

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;
    }
}
like image 59
Frank Avatar answered Nov 13 '22 00:11

Frank


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".

like image 27
Himanshu Dhakecha Avatar answered Nov 13 '22 00:11

Himanshu Dhakecha


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" />
like image 35
M P Mathugama Avatar answered Nov 13 '22 00:11

M P Mathugama