Android Network State Check isNetworkReady(Context context)

Here you can find the source of isNetworkReady(Context context)

Description

Check the network interface (system-level) connectivity ATTENTION: permissions must be set in manifest.xml

Parameter

Parameter Description
context a parameter

Declaration

public static boolean isNetworkReady(Context context) 

Method Source Code

//package com.java2s;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Main {
    /**// w w  w. j  av a2  s .c o m
     * Check the network interface (system-level) connectivity
     * ATTENTION: permissions must be set in manifest.xml
     * @param context
     * @return
     */
    public static boolean isNetworkReady(Context context) {

        boolean status = false;
        try {
            ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getNetworkInfo(0);
            if (netInfo != null
                    && netInfo.getState() == NetworkInfo.State.CONNECTED) {
                status = true;
            } else {
                netInfo = cm.getNetworkInfo(1);
                if (netInfo != null
                        && netInfo.getState() == NetworkInfo.State.CONNECTED)
                    status = true;
            }
        } catch (Exception e) {
            //e.printStackTrace();  
            return false;
        }
        return status;

    }
}

Related

  1. isConnectionFast(int type, int subType)
  2. isNetworkAvailable()
  3. isNetworkConnected(Context context)
  4. isOnline(Context c)
  5. isOnline(Context context)
  6. isConMeNetwork(String ssid)
  7. isConnectNetWork(final Context context)
  8. isOnline(Context context)
  9. isNetworkAvailable(Context context)