Example usage for android.net NetworkInfo getState

List of usage examples for android.net NetworkInfo getState

Introduction

In this page you can find the example usage for android.net NetworkInfo getState.

Prototype

@Deprecated
public State getState() 

Source Link

Document

Reports the current coarse-grained state of the network.

Usage

From source file:org.brandroid.openmanager.fragments.DialogHandler.java

private static String getNetworkInfoInfo(NetworkInfo info) {
    String ret = "";
    ret += info.getSubtypeName() + "/ ";
    if (info.getState() != null)
        ret += "s=" + info.getState().name() + "/ ";
    if (info.getDetailedState() != null)
        ret += "d=" + info.getDetailedState().name() + "/ ";
    if (info.getExtraInfo() != null)
        ret += "e=" + info.getExtraInfo();
    return ret;/*  w  w w . j  a  v a  2  s . c  o  m*/
}

From source file:org.mewx.wenku8.global.GlobalConfig.java

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm != null) {
        NetworkInfo[] info = cm.getAllNetworkInfo();
        if (info != null) {
            for (NetworkInfo ni : info) {
                if (ni.getState() == NetworkInfo.State.CONNECTED)
                    return true;
            }/*  w  ww  . j  a  v  a2 s .  co m*/
        }
    }
    return false;
}

From source file:com.anyonavinfo.commonuserregister.MainActivity.java

/**
 * ?//from w  ww.  ja  v a 2  s.com
 */
public static boolean isConnected(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (null != connectivity) {
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (null != info && info.isConnected()) {
            if (info.getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }
        }
    }
    return false;
}

From source file:org.metawatch.manager.Monitors.java

private static void createWifiReceiver(final Context context) {
    if (wifiReceiver != null)
        return;//from   www .jav a  2  s  . c  o m
    WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wm.getConnectionInfo();
    if (info != null)
        SignalData.wifiBars = 1 + WifiManager.calculateSignalLevel(info.getRssi(), 4);
    wifiReceiver = new BroadcastReceiver() {
        int wifiBars = 0;

        @Override
        public void onReceive(Context c, Intent intent) {
            String action = intent.getAction();
            if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
                if (intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
                        WifiManager.WIFI_STATE_UNKNOWN) != WifiManager.WIFI_STATE_ENABLED) {
                    wifiBars = 0;
                }
            } else if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
                if (!intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)) {
                    wifiBars = 0;
                }
            } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
                NetworkInfo netInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
                if (netInfo.getState() != NetworkInfo.State.CONNECTED) {
                    wifiBars = 0;
                } else {
                    WifiInfo wifiInfo = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
                    if (wifiInfo == null) {
                        wifiBars = 0;
                    } else {
                        wifiBars = 1 + WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 4);
                    }
                }
            } else if (action.equals(WifiManager.RSSI_CHANGED_ACTION)) {
                final int newRssi = intent.getIntExtra(WifiManager.EXTRA_NEW_RSSI, -200);
                wifiBars = 1 + WifiManager.calculateSignalLevel(newRssi, 4);
            }
            if (wifiBars != SignalData.wifiBars) {
                SignalData.wifiBars = wifiBars;
                Idle.updateIdle(context, true);
            }
        }
    };
    IntentFilter f = new IntentFilter();
    f.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
    f.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
    f.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    f.addAction(WifiManager.RSSI_CHANGED_ACTION);
    context.registerReceiver(wifiReceiver, f);
}

From source file:com.hkm.Application.appWork.java

public boolean isNetworkOnline() {
    boolean status = false;
    try {/*from   www  .ja v  a2  s.c  om*/
        ConnectivityManager cm = (ConnectivityManager) 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();
        Tool.trace(getApplicationContext(), e.toString());
        return false;
    }
    return status;
}

From source file:com.OpenSource.engine.connectivity.ConnectivityInfoProvider.java

public boolean isWiFiConnected() {
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo == null) {
        return false;
    }/*www.ja v  a2  s .co m*/
    return networkInfo.getState() == NetworkInfo.State.CONNECTED
            && networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
}

From source file:com.OpenSource.engine.connectivity.ConnectivityInfoProvider.java

public boolean isMobileConnected() {
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo == null) {
        return false;
    }/*from   www .ja  v  a  2  s . c om*/
    return networkInfo.getState() == NetworkInfo.State.CONNECTED
            && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
}

From source file:com.github.sryze.wirebug.MainActivity.java

@Override
protected void onResume() {
    super.onResume();

    boolean isConnected = NetworkUtils.isConnectedToWifi(connectivityManager);
    connectedView.setVisibility(isConnected ? View.VISIBLE : View.GONE);
    notConnectedView.setVisibility(isConnected ? View.GONE : View.VISIBLE);

    boolean isEnabled = DebugManager.isTcpDebuggingEnabled();
    updateInstructions(isEnabled);//from w w  w.  jav  a 2s .  c o  m
    updateStatus();

    toggleDebuggingButton.setOnCheckedChangeListener(null);
    toggleDebuggingButton.setChecked(isEnabled);
    toggleDebuggingButton.setOnCheckedChangeListener(enableSwitchChangeListener);

    networkStateChangedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "Received network state changed broadcast");
            NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            switch (networkInfo.getState()) {
            case CONNECTED:
                connectedView.setVisibility(View.VISIBLE);
                notConnectedView.setVisibility(View.INVISIBLE);
                break;
            case DISCONNECTED:
                connectedView.setVisibility(View.GONE);
                notConnectedView.setVisibility(View.VISIBLE);
                break;
            }
            updateConnectionInfo();
        }
    };
    registerReceiver(networkStateChangedReceiver, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));

    debugStatusChangedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "Received debug state change broadcast");
            boolean isEnabled = intent.getBooleanExtra(DebugStatusService.EXTRA_IS_ENABLED, false);
            updateInstructions(isEnabled);
            toggleDebuggingButton.setOnCheckedChangeListener(null);
            toggleDebuggingButton.setChecked(isEnabled);
            toggleDebuggingButton.setOnCheckedChangeListener(enableSwitchChangeListener);
        }
    };
    registerReceiver(debugStatusChangedReceiver, new IntentFilter(DebugStatusService.ACTION_STATUS_CHANGED));
}

From source file:com.OpenSource.engine.connectivity.ConnectivityInfoProvider.java

public boolean isConnected(int waitForSecondsIfConnecting) {
    // validate range for waitForSecondsIfConnecting
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo == null) {
        return false;
    }/* ww  w  .  j a  v a  2 s  .c  o  m*/

    while (waitForSecondsIfConnecting > 0 && networkInfo.getState() == NetworkInfo.State.CONNECTING) {
        try {
            Thread.sleep(2 * 1000);
            waitForSecondsIfConnecting = waitForSecondsIfConnecting - 2;
        } catch (InterruptedException e) {
        }
    }
    return networkInfo.isConnected();
}

From source file:com.OpenSource.engine.connectivity.ConnectivityInfoProvider.java

private void notifyListeners(NetworkInfo networkInfo) {
    if (connectivityListenersList != null) {
        for (IConnectivityListener connectivityListener : connectivityListenersList) {
            switch (networkInfo.getState()) {
            case CONNECTED:
                connectivityListener.onConnected(networkInfo.getType());
                if (networkInfo.isRoaming()) {
                    connectivityListener.onRoaming();
                }/*from  w w  w.ja v  a 2  s  .c o m*/
                break;
            case CONNECTING:
                connectivityListener.onConnecting(networkInfo.getType());
                break;
            case DISCONNECTED:
                connectivityListener.onDisconnected(networkInfo.getType());
                break;
            case DISCONNECTING:
                connectivityListener.onDisconnecting(networkInfo.getType());
                break;
            case UNKNOWN:
                break;
            }
        }
    }
}