0

I use android device

and I want when app start, check current network state.

so, I try this.

ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERIVCE);
NetworkInfo ethernet = manager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (wifi.isConnected()) {
   Log.d(TAG, "WIFI isConnected");
} else if (ethernet.isConnected()) {
   Log.d(TAG, "Ethernet isConnected");
} else if (wifi.isAvaiable()) {
   Log.d(TAG, "Wifi not connect, but wifi isAvailable");
} else {
   Log.d(TAG, "not available network");
}

this source check Wi-Fi and Ethernet network state.

but when unable network. not check.

My device if use wifi, connect Wireless LAN on my device.

when disconnect Wireless Lan on my device. my source is Check wifi.isAvailable()

in conclusion,

  1. How to check when network is not available (current source, when network is not available check wifi.isAvailable)

  2. When connect Wireless Lan and Wifi off, check wifi.isAvailable() but When disconnect Wireless Lan and Wifi off , check wifi.isAvailable() How to distinguish from this situation.

THANKS.

chohyunwook
  • 203
  • 2
  • 3
  • 14

7 Answers7

0

you should use getActiveNetworkInfo() to check connectivity of currently active default data network--no need to check for particular data network; be it Ethernet or Wi-Fi.

  public static boolean NetAvailable(final Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    final boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    return isConnected;
   }
Jay
  • 2,852
  • 1
  • 15
  • 28
0

Try this function

public boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

and check like this

if(isNetworkAvailable(this))
{
System.out.println(""Internet Connected);
}else
{
System.out.println(""Internet Not Connected);
}
AbhayBohra
  • 2,047
  • 24
  • 36
0
public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(
                Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
Jordon
  • 191
  • 1
  • 12
0

For knowing working internet state use this snippet

public static boolean isInternetAccess() {
    try {
        HttpURLConnection urlc = (HttpURLConnection) 
            (new URL("http://www.google.com")
            .openConnection());
        urlc.setRequestProperty("User-Agent", "Android");
        urlc.setRequestProperty("Connection", "close");
        urlc.setConnectTimeout(1500); 
        urlc.connect();
        return (urlc.getResponseCode() == 200);
    } catch (IOException e) {
        Log.e(TAG, "Error checking internet connection", e);
    }

return false;

}

If this code returns failed result that means network is not available,and call your code for getting available networks.

Shrikant
  • 247
  • 1
  • 13
0

you need to use BroadcastReceiver for check connectivity. when status change it call onReceive() method.

public BroadcastReceiver internetConnectionReciever = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        @SuppressWarnings("static-access")
        NetworkInfo activeWIFIInfo = connectivityManager
                .getNetworkInfo(connectivityManager.TYPE_WIFI);

        if (activeWIFIInfo.isConnected() || activeNetInfo.isConnected()) {

        } 
    }
};

Register Receiver in onResume() and destroy it in onDestroy()

 @Override
protected void onResume() {
    super.onResume();
    registerReceiver(internetConnectionReciever, new IntentFilter(
            "android.net.conn.CONNECTIVITY_CHANGE"));

}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(internetConnectionReciever);
}
0
   public static boolean checkInternetConnection(Context context) {
        try {
            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);
            android.net.NetworkInfo ethernet = connec.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);

            if ((wifi != null && wifi.isConnected())
                    || (mobile != null && mobile.isConnected())
                    || (ethernet != null && ethernet.isConnected())) {
                return true;
            }
            log("Connection", "Connection failed");
            return false;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return false;
        }
    }   

Use this method. you don't need to check wifi.isAvailable(), just check isConnected()

-2

Try this for checking device online or offline.

private boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    if (netInfo != null && netInfo.isConnectedOrConnecting()){
        return true;
    }else return false;
}
Jakir Hossain
  • 800
  • 6
  • 12