1

How can i check that internet has or not. But i found this code but when wifi connected it return true, but i know that there is not internet. No internet access.

 public boolean chechInternet_con(){
    ConnectivityManager connec = (ConnectivityManager)
    context.getSystemService(Context.CONNECTIVITY_SERVICE);
    android.net.NetworkInfo wifi =connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    // Here if condition check for wifi and mobile network is available or not.
    // If anyone of them is available or connected then it will return true, otherwise false;

    if (wifi.isConnected() || mobile.isConnected()) {
        return true;
    } 
    return false;

}
uzyn
  • 6,625
  • 5
  • 22
  • 41
Qafqaz Qafqaz
  • 89
  • 1
  • 2
  • 8

3 Answers3

1

Try this,

public boolean isInternetOn(Context ctx) {
        this.mContext = ctx;
        ConnectivityManager Connect_Manager = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        State connected = NetworkInfo.State.CONNECTED;
        State connecting = NetworkInfo.State.CONNECTING;
        State disconnected = NetworkInfo.State.DISCONNECTED;

        State info0 = Connect_Manager.getNetworkInfo(0).getState();
        State info1 = Connect_Manager.getNetworkInfo(1).getState();

        // ARE WE CONNECTED TO THE NET
        if (info0 == connected || info0 == connecting || info1 == connecting
                || info1 == connected) {

            Log.d("Internet", "Connected");
            return true;
        } else if (info0 == disconnected || info1 == disconnected) {
            Log.d("Internet", "DisConnected");
            return false;
        }
        return false;
    }
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
0

See below code

    public static boolean checkConnection(Context context) {

    final ConnectivityManager mConnectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    final NetworkInfo netInfo = mConnectivityManager.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    } else
        return false;
}
Nirali
  • 13,571
  • 6
  • 40
  • 53
  • 1
    Thank But you only are checking connected or connecting but my device connected to wifi but no internet access. i mean i can not enter any sites:( – Qafqaz Qafqaz Jul 26 '12 at 05:20
0

So, its connected to wi-fi network but no internet access. Right?

Try to ping 8.8.8.8 or google.com to know whether there is internet access. This will help you.

Community
  • 1
  • 1
Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109