Example usage for android.net ConnectivityManager getAllNetworkInfo

List of usage examples for android.net ConnectivityManager getAllNetworkInfo

Introduction

In this page you can find the example usage for android.net ConnectivityManager getAllNetworkInfo.

Prototype

@Deprecated
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
@NonNull
public NetworkInfo[] getAllNetworkInfo() 

Source Link

Document

Returns connection status information about all network types supported by the device.

Usage

From source file:jieehd.villain.updater.VillainUpdater.java

private boolean haveNetworkConnection() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;

    }//  ww  w.j  av  a2  s.co  m
    if (haveConnectedWifi == false && haveConnectedMobile == false) {
        Log.d("Network State", "false");
        AlertDialog.Builder alert = new AlertDialog.Builder(VillainUpdater.this);
        alert.setTitle("No Data Connection!");
        alert.setMessage(
                "You have no data connection, click ok to turn on WiFi or Mobile Data in order to check for OTA updates.");
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) {
                // TODO Auto-generated method stub
                final Intent intent = new Intent(Intent.ACTION_MAIN, null);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                final ComponentName cn = new ComponentName("com.android.settings",
                        "com.android.settings.wifi.WifiSettings");
                intent.setComponent(cn);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }

        });
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                return;
            }
        });
        alert.show();

    } else {
        download();
    }
    return haveConnectedWifi || haveConnectedMobile;
}

From source file:edu.wpi.khufnagle.lighthousenavigator.PhotographsActivity.java

/**
 * Determines whether the user can connect to the Internet using a Wi-Fi
 * network or over a cellular network./*  www.  j  a  va2  s . com*/
 * @return True if the user has either type of Internet connection
 */
/*
 * Courtesy of
 * http://stackoverflow.com/questions/4238921/android-detect-whether
 * -there-is-an-internet-connection-available
 */
private boolean networkConnectionExists() {
    boolean wifiConnectionExists = false;
    boolean mobileConnectionExists = false;

    final ConnectivityManager connManager = (ConnectivityManager) this
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo[] connSources = connManager.getAllNetworkInfo();
    for (final NetworkInfo connSource : connSources) {
        if (connSource.getTypeName().equalsIgnoreCase("WIFI")) {
            if (connSource.isConnected()) {
                wifiConnectionExists = true;
            }
        }

        if (connSource.getTypeName().equalsIgnoreCase("MOBILE")) {
            if (connSource.isConnected()) {
                mobileConnectionExists = true;
            }
        }
    }

    return wifiConnectionExists || mobileConnectionExists;
}

From source file:com.lemon.lime.MainActivity.java

private boolean isconnected() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();

    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }/*from w  w  w .jav  a  2  s .  c om*/
    return haveConnectedWifi || haveConnectedMobile;
}

From source file:RhodesService.java

private static boolean hasNetworkEx(boolean checkCell, boolean checkWifi, boolean checkEthernet,
        boolean checkWimax, boolean checkBluetooth, boolean checkAny) {
    if (!Capabilities.NETWORK_STATE_ENABLED) {
        Logger.E(TAG, "HAS_NETWORK: Capability NETWORK_STATE disabled");
        return false;
    }/*from   w  w  w  .j a v  a2 s. c om*/

    Context ctx = RhodesService.getContext();
    ConnectivityManager conn = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (conn == null) {
        Logger.E(TAG, "HAS_NETWORK: cannot create ConnectivityManager");
        return false;
    }

    NetworkInfo[] info = conn.getAllNetworkInfo();
    if (info == null) {
        Logger.E(TAG, "HAS_NETWORK: cannot issue getAllNetworkInfo");
        return false;
    }

    //{
    //   Utils.platformLog("NETWORK", "$$$$$$$$$$$$$$$$$$$   Networks ; $$$$$$$$$$$$$$$$$$$$$$");
    //   for (int i = 0, lim = info.length; i < lim; ++i) 
    //   {
    //      int type = info[i].getType();
    //      String name = info[i].getTypeName();
    //      boolean is_connected = info[i].getState() == NetworkInfo.State.CONNECTED;
    //      Utils.platformLog("NETWORK", "        - Name ["+name+"],  type ["+String.valueOf(type)+"], connected ["+String.valueOf(is_connected)+"]");
    //   }
    //}

    for (int i = 0, lim = info.length; i < lim; ++i) {
        boolean is_connected = info[i].getState() == NetworkInfo.State.CONNECTED;
        int type = info[i].getType();
        if (is_connected) {
            if ((type == ConnectivityManager.TYPE_MOBILE) && (checkCell))
                return true;
            if ((type == ConnectivityManager.TYPE_WIFI) && (checkWifi))
                return true;
            if ((type == 6) && (checkWimax))
                return true;
            if ((type == 9) && (checkEthernet))
                return true;
            if ((type == 7) && (checkBluetooth))
                return true;
            if (checkAny)
                return true;
        }
    }

    Logger.I(TAG, "HAS_NETWORK: all networks are disconnected");

    return false;
}

From source file:it.sasabz.android.sasabus.fragments.OnlineSearchFragment.java

/**
* this method checks if a networkconnection is active or not
* @return boolean if the network is reachable or not
*///from   ww w .  j av  a  2  s. c  o  m
private boolean haveNetworkConnection() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) (getActivity()
            .getSystemService(Context.CONNECTIVITY_SERVICE));
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        //testing WIFI connection
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        //testing GPRS/EDGE/UMTS/HDSPA/HUSPA/LTE connection
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;
}

From source file:com.gelakinetic.mtgfam.FamiliarActivity.java

/**
 * Checks the networks state/*from   ww w .  j  a v  a2 s.  c o  m*/
 *
 * @param context         the context where this is being called
 * @param shouldShowToast true, if you want a Toast to be shown indicating a lack of network
 * @return -1 if there is no network connection, or the type of network, like ConnectivityManager.TYPE_WIFI
 */
public static int getNetworkState(Context context, boolean shouldShowToast) {
    try {
        ConnectivityManager conMan = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        for (NetworkInfo ni : conMan.getAllNetworkInfo()) {
            if (ni.isConnected()) {
                return ni.getType();
            }
        }
        if (shouldShowToast) {
            ToastWrapper.makeText(context, R.string.no_network, ToastWrapper.LENGTH_SHORT).show();
        }
        return -1;
    } catch (NullPointerException e) {
        if (shouldShowToast) {
            ToastWrapper.makeText(context, R.string.no_network, ToastWrapper.LENGTH_SHORT).show();
        }
        return -1;
    }
}

From source file:com.ti.sensortag.gui.services.ServicesActivity.java

private void checkInternetConenction() {
    ConnectivityManager check = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (check != null) {
        NetworkInfo[] info = check.getAllNetworkInfo();
        if (info != null)
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    choice = 1;/*from  w  ww .  ja  v  a2s .co  m*/
                    Toast.makeText(this, "Internet is connected", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
    }
    Toast.makeText(this, "Not conencted to internet", Toast.LENGTH_SHORT).show();
    choice = 2;
    return;

}

From source file:de.androvdr.activities.AndroVDR.java

private void togglePortforwarding() {
    new CloseConnectionTask().execute(CLOSE_CONNECTION);

    if (Preferences.useInternet == false) {
        String connectionState = "";
        final ConnectivityManager cm = (ConnectivityManager) this
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] info = cm.getAllNetworkInfo();
        for (int i = 0; i < info.length; i++) {
            if (info[i].isConnected() == true) { // dann wird wohl hoffentlich eine Verbindung klappen
                portForwarding = new PortForwarding(sshDialogHandler, this);
                return;
            } else { // sammel mer mal die Begruendung
                NetworkInfo.DetailedState state = info[i].getDetailedState();
                connectionState += info[i].getTypeName() + " State is " + state.name() + "\n";
            }/* ww w. j  a  v a2s .c o m*/
        }
        // kein Netzwerk vorhanden, also mach mer nix
        Toast.makeText(this, connectionState, Toast.LENGTH_LONG).show();
    } else {
        // Toast.makeText(Settings.this, "Nein", Toast.LENGTH_SHORT).show();
        if (portForwarding != null) {
            portForwarding.disconnect();
            portForwarding = null;
        }
    }
}

From source file:com.att.arocollector.AROCollectorActivity.java

/**
 * @return/*from w  ww.jav a 2s .  c  o  m*/
 */
private boolean isConnectedToInternet() {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null)
            for (int j = 0; j < info.length; j++) {
                Log.i(TAG, "NETWORK CONNECTION : " + info[j].getState() + " Connected STATE :"
                        + NetworkInfo.State.CONNECTED);
                if (info[j].getState().equals(NetworkInfo.State.CONNECTED)) {
                    return true;
                }
            }
    }
    return false;
}

From source file:com.fine47.http.ActivityHttpClient.java

/**
 * Checks whether the system is online (connected to a network) or not.
 *
 * @return TRUE if the system is online, FALSE otherwise
 *//*from   www .  j av  a  2s .c om*/
public boolean isOnline() {
    try {
        ConnectivityManager cm = (ConnectivityManager) getContext().getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (null != netInfo) {
            // Check for availability and if it's really connected.
            isConnected = netInfo.isAvailable() && netInfo.isConnectedOrConnecting();

            // Get available networks' info.
            NetworkInfo[] netsInfo = cm.getAllNetworkInfo();

            // What kind of networks are available.
            for (NetworkInfo ni : netsInfo) {
                if (ni.isConnected()) {
                    String niType = ni.getTypeName();
                    if ("WIFI".equalsIgnoreCase(niType)) {
                        isWifiConnected = true;
                    } else if ("MOBILE".equalsIgnoreCase(niType)) {
                        isMobileConnected = true;
                    }
                }
            }
        } else {
            isConnected = false;
        }

        return isConnected;
    } catch (Throwable error) {
        if (isDebugging()) {
            Log.e(LOG_TAG, "Error while detecting network status.", error);
        }
    }

    return isConnected;
}