Example usage for android.net ConnectivityManager EXTRA_NETWORK_INFO

List of usage examples for android.net ConnectivityManager EXTRA_NETWORK_INFO

Introduction

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

Prototype

String EXTRA_NETWORK_INFO

To view the source code for android.net ConnectivityManager EXTRA_NETWORK_INFO.

Click Source Link

Document

The lookup key for a NetworkInfo object.

Usage

From source file:Main.java

static public boolean isDisconnectedIntent(Intent intent) {
    boolean res = false;
    NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
    if (networkInfo != null) {
        NetworkInfo.State state = networkInfo.getState();
        res = (state.equals(NetworkInfo.State.DISCONNECTING) || state.equals(NetworkInfo.State.DISCONNECTED))
                && (networkInfo.getType() == ConnectivityManager.TYPE_WIFI);

    } else {// w  w w.  j a  va  2  s  .co  m
        int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
        if (wifiState == WifiManager.WIFI_STATE_DISABLED || wifiState == WifiManager.WIFI_STATE_DISABLING) {
            res = true;
        }
    }
    return res;
}

From source file:net.primeranks.fs_viewer.fs_replay.ConnectionChangedBroadcastReceiver.java

public void onReceive(Context context, Intent intent) {
    String info = intent.getStringExtra(ConnectivityManager.EXTRA_EXTRA_INFO);
    NetworkInfo nwInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
    Log.d(Config.LOG_AS, info + ": " + nwInfo.getReason());

    HttpParams httpParams = EntryPointActivity.getHttpClient().getParams();
    if (nwInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
        String proxyHost = Proxy.getHost(context);
        if (proxyHost == null) {
            proxyHost = Proxy.getDefaultHost();
        }//from   w  w w. j av  a2s .  c om
        int proxyPort = Proxy.getPort(context);
        if (proxyPort == -1) {
            proxyPort = Proxy.getDefaultPort();
        }
        if (proxyHost != null && proxyPort > -1) {
            HttpHost proxy = new HttpHost(proxyHost, proxyPort);
            httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        } else {
            httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, null);
        }
    } else {
        httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, null);
    }
}

From source file:net.helff.wificonnector.WifiConnectivityReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    Log.i(TAG, "action: " + intent.getAction());

    if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {

        Intent locationIntent = new LocationIntent();
        context.sendBroadcast(locationIntent);
    }//  w  w  w  .  ja v  a2  s. c o m

    if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {

        NetworkInfo networkInfo = (NetworkInfo) intent
                .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

        Log.d(TAG, "got network_state_changed with detailed info: " + networkInfo == null ? "nothing"
                : networkInfo.getDetailedState().name());

        if (networkInfo != null && networkInfo.isConnected()
                && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
            boolean autoConnect = prefs.getBoolean("autoConnect", false);

            Log.d(TAG, "triggering WifiConnectivityService with autoConnect=" + autoConnect);

            int command = autoConnect ? WifiConnectivityService.COMMAND_AUTO_UNLOCK_CONNECTION
                    : WifiConnectivityService.COMMAND_CHECK_CONNECTION;
            Intent msgIntent = new Intent(context, WifiConnectivityService.class);
            msgIntent.putExtra(WifiConnectivityService.INTENT_COMMAND, command);
            context.startService(msgIntent);
        }

    }
    if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
        Log.d(TAG, "triggering WifiConnectivityService state changed");

        Intent msgIntent = new Intent(context, WifiConnectivityService.class);
        msgIntent.putExtra(WifiConnectivityService.INTENT_COMMAND,
                WifiConnectivityService.COMMAND_REFRESH_STATUS);
        context.startService(msgIntent);
    }
}

From source file:com.drinviewer.droiddrinviewer.DrinViewerBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
        NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if (networkInfo.isConnected()) {
            /**//w w  w.j a va2s . c  om
             * WiFi is connected, get its broadcast 
             * address and start the discovery process
             * repeated at a fixed time interval
             */
            wifiBroadcastAddress = getWiFiBroadcastAddress(context);
            startAlarmRepeater(context);
        } else {
            wifiBroadcastAddress = null;
        }
    } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
        NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI && !networkInfo.isConnected()) {
            /**
             * WiFi is disconnected, stop the discovery
             * process repeating, it would be a waste of resources
             */
            wifiBroadcastAddress = null;
            stopAlarmRepeater(context);
        }
    } else if (intent.getAction().equals(context.getResources().getString(R.string.broadcast_startdiscovery))
            || intent.getAction()
                    .equals(context.getResources().getString(R.string.broadcast_cleanhostcollection))) {

        boolean startService = true;
        /**
         * Calls the DiscoverServerService asking to do a discovery
         * or a clean host collection by simply forwarding the received action
         */
        Intent service = new Intent(context, DiscoverServerService.class);
        service.setAction(intent.getAction());

        if (intent.getAction().equals(context.getResources().getString(R.string.broadcast_startdiscovery))) {

            ConnectivityManager connManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            wifiBroadcastAddress = (mWifi.isConnected()) ? getWiFiBroadcastAddress(context) : null;
            startService = wifiBroadcastAddress != null;
            service.putExtra("wifiBroadcastAddress", wifiBroadcastAddress);
        }

        if (startService)
            startWakefulService(context, service);

        if (intent.getBooleanExtra("stopservice", false)) {
            context.stopService(service);
        }
    } else if (intent.getAction()
            .equals(context.getResources().getString(R.string.broadcast_startalarmrepeater))) {
        /**
         * start the alarm repeater only if WiFi is connected already
         * used by ServerListFragment.onServiceConnected method to start the discovery
         * if the application is launched being already connected to a WiFi network
         */
        ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (mWifi.isConnected()) {
            // if we're called from the activity, try to get a broadcast address
            if (intent.getBooleanExtra("forcegetbroadcast", false))
                wifiBroadcastAddress = getWiFiBroadcastAddress(context);
            startAlarmRepeater(context);
        } else {
            wifiBroadcastAddress = null;
        }
    } else if (intent.getAction()
            .equals(context.getResources().getString(R.string.broadcast_stopalarmrepeater))) {
        /**
         *  stop the alarm repeater. period.
         *  used by DrinViewerApplication.onTerminate method
         */
        wifiBroadcastAddress = null;
        stopAlarmRepeater(context);
    }
}

From source file:org.skt.runtime.html5apis.NetworkManager.java

/**
 * Sets the context of the Command. This can then be used to do things like
 * get file paths associated with the Activity.
 * /*from  w ww .  ja v a 2s . c  om*/
 * @param ctx The context of the main Activity.
 */
public void setContext(RuntimeInterface ctx) {
    super.setContext(ctx);
    this.sockMan = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    this.connectionCallbackId = null;

    // We need to listen to connectivity events to update navigator.connection
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    if (this.receiver == null) {
        this.receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                updateConnectionInfo(
                        (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO));
            }
        };
        ctx.registerReceiver(this.receiver, intentFilter);
    }

}

From source file:ar.com.iron.android.extensions.connections.InetDataClient.java

/**
 * Invocado cuando hay cambios de conectividad en la plataforma
 * /*from  w  w w. ja  v a  2s  . c o  m*/
 * @param intent
 *            Intent con los datos del cambio
 */
protected void onConnectivityChanged(Intent intent) {
    boolean connectivityLoosed = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
    boolean hadConnectivity = this.hasConnectivity.get();
    this.hasConnectivity.set(!connectivityLoosed);
    if (connectivityListener != null && hadConnectivity == connectivityLoosed) {
        // Es un cambio de estado, tenemos que notificarlo
        NetworkInfo network = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        connectivityListener.onConnectivityChanged(!connectivityLoosed, network);
    }
}

From source file:com.polyvi.xface.extension.XNetworkConnectionExt.java

/**
 * ? plugin  NetworkConnection ./* w w  w.jav a  2s  .co  m*/
 *  Android ??BroadcastReceiver Android  broadcast
 *
 */
public void initConnectionContext() {
    Context context = getContext();
    this.mSockMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    // ????connection
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    if (this.mReceiver == null) {
        this.mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                updateConnectionInfo(
                        (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO));
            }
        };
        context.registerReceiver(this.mReceiver, intentFilter);
    }
}

From source file:org.eeiiaa.wifi.WifiConnector.java

@Override
    public void onReceive(Context context, Intent intent) {
        // handle stuff related to scanning requested when:
        // normal scan is issued
        // when forgetting a network and re-connecting to a previously configured
        // existing one
        // when connecting and need a scan to verify connection can be established
        Log.i(TAG, "action: " + intent.getAction());
        if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) && (mWaitingScan || mConnecting)) {
            // forgetting current and reconnecting to highest priority existing
            // network

            Log.i(TAG, "ACTION SCAN: mWaitingScan:" + mWaitingScan + " connecting " + mConnecting);

            // normal scan request
            if (mWaitingScan) {
                String json = new String();
                switch (scanResultOption) {
                case GET_ALL:
                    json = getScannedNetworksAllJSON();
                    break;
                case GET_BSSID_ONLY:
                    json = getScannedNetworksJSON();
                    break;
                }/* w w  w  . j a  v a  2 s  .co m*/
                mScanListener.scanResultsJSON(json);
                mWaitingScan = false;

                // scan is done - unregister
                mContext.unregisterReceiver(this);
                Log.i(TAG, "processing scan results as JSON");

            }

            if (mConnecting && !mWaitingConnection) {
                // look for the network we want to connect to in the scanned results
                ScanResult scannedNet = searchNetwork(mNetssid);
                if (scannedNet == null) {
                    Log.i(TAG, "ssid not found...: " + mNetssid);
                    mConnecting = false;
                    mContext.unregisterReceiver(this);
                    notifyConnectionFailed();
                    return; // didn't find requested network noting to connect
                }

                WifiConfiguration configuredNet = searchConfiguration(scannedNet);
                boolean result_ok = false;

                if (configuredNet != null) {
                    // configuration exits connect to a configured network
                    mWaitingConnection = true;
                    result_ok = Wifi.connectToConfiguredNetwork(mContext, mWifiMgr, configuredNet, false);
                    Log.i(TAG, "configuration exits connect to a configured network");
                } else {
                    // configure and connect to network
                    mWaitingConnection = true;
                    result_ok = Wifi.connectToNewNetwork(mContext, mWifiMgr, scannedNet, mPwd, MAX_OPEN_NETS);
                    Log.i(TAG, "configure and connect to network");
                }

                // failed to connect - unregister and notify
                if (!result_ok) {
                    mContext.unregisterReceiver(this);
                    mWaitingConnection = false;
                    mConnecting = false;
                    Log.e(TAG, "error connecting Wifi.connect returned error");
                    notifyConnectionFailed();
                }
            }
        }

        else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)
                && (mConnected || (mConnecting && mWaitingConnection))) {
            Log.i(TAG, "ACTION CONNECTIVITY: mWaitingScan:" + mWaitingScan + " forgetting: " + forgetting_
                    + " connecting " + mConnecting);

            NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

            if (!mConnected && networkInfo.getType() == ConnectivityManager.TYPE_WIFI
                    && networkInfo.isConnected()) {

                if (mWifiMgr.getConnectionInfo().getSupplicantState() == SupplicantState.COMPLETED) {

                    Log.i(TAG, " getConnectionInfo: " + mWifiMgr.getConnectionInfo() + " getSSID: "
                            + mWifiMgr.getConnectionInfo().getSSID());
                    String connectionSsid = unQuote(mWifiMgr.getConnectionInfo().getSSID());
                    // when phone turns into AP mode, wifimgr returns null...
                    // fail protection for such cases...
                    if (connectionSsid != null) {
                        Log.i(TAG, "mNetssid: |" + mNetssid + "| connectionSsid |" + connectionSsid + "|");
                        if (connectionSsid.equals(mNetssid)) {
                            mConnected = true;
                            mConnecting = false;
                            mWaitingConnection = false;
                            forgetting_ = false;

                            Log.i(TAG, "VERIFIED SUCCESSFUL CONNECTION to: " + mNetssid);
                            // connection done notify - do not unregister to get disconnection detected
                            notifyConnectionListener();
                        }
                    }
                }
            } else if (mConnected && networkInfo.getType() == ConnectivityManager.TYPE_WIFI
                    && !networkInfo.isConnected()) {

                Log.i(TAG, "network is disconnected...");

                // Wifi is disconnected
                mConnected = false;
                mWaitingConnection = false;
                if (mDataListener != null && used_ != null) {
                    used_.updateUsageCounters();
                    transmitted = used_.getWifiTransmitted();
                    received = used_.getWifiReceived();
                }
                // network lost was not expected (when forgetting we expect network to
                // be lost)
                if (!forgetting_) {
                    Log.i(TAG, "network lost " + networkInfo);
                    notifyNetworkLost(networkInfo);
                    mContext.unregisterReceiver(this);
                } else {
                    Log.i(TAG, "network lost when forgetting " + networkInfo);
                }
            }

            Log.i(TAG, "end of CONNECTIVITY_ACTION");
        }

    }

From source file:com.moez.QKSMS.mmssms.Transaction.java

private void sendMMS(final byte[] bytesToSend) {
    revokeWifi(true);//from   w  w  w .  jav a 2 s.com

    // enable mms connection to mobile data
    mConnMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    int result = beginMmsConnectivity();

    if (LOCAL_LOGV)
        Log.v(TAG, "result of connectivity: " + result + " ");

    if (result != 0) {
        // if mms feature is not already running (most likely isn't...) then register a receiver and wait for it to be active
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        final BroadcastReceiver receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context1, Intent intent) {
                String action = intent.getAction();

                if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                    return;
                }

                @SuppressWarnings("deprecation")
                NetworkInfo mNetworkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

                if ((mNetworkInfo == null) || (mNetworkInfo.getType() != ConnectivityManager.TYPE_MOBILE)) {
                    return;
                }

                if (!mNetworkInfo.isConnected()) {
                    return;
                } else {
                    // ready to send the message now
                    if (LOCAL_LOGV)
                        Log.v(TAG, "sending through broadcast receiver");
                    alreadySending = true;
                    sendData(bytesToSend);

                    context.unregisterReceiver(this);
                }

            }

        };

        context.registerReceiver(receiver, filter);

        try {
            Looper.prepare();
        } catch (Exception e) {
            // Already on UI thread probably
        }

        // try sending after 3 seconds anyways if for some reason the receiver doesn't work
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!alreadySending) {
                    try {
                        if (LOCAL_LOGV)
                            Log.v(TAG, "sending through handler");
                        context.unregisterReceiver(receiver);
                    } catch (Exception e) {

                    }

                    sendData(bytesToSend);
                }
            }
        }, 7000);
    } else {
        // mms connection already active, so send the message
        if (LOCAL_LOGV)
            Log.v(TAG, "sending right away, already ready");
        sendData(bytesToSend);
    }
}

From source file:com.ubiLive.GameCloud.Browser.WebBrowser.java

private void processNetworkType(Intent intent, Context context) {
    int netType;// w  ww  .ja  va2 s. co m
    int netSubtype;
    int networkType;
    boolean isConnected;
    String reloadUrl;
    DebugLog.d(TAG, "processNetworkType() enter");

    NetworkInfo netInfo = Utils.getCurNetworkInfo(context);
    if (netInfo == null || netInfo.isAvailable() == false) {
        DebugLog.d(TAG, "getCurNetworkInfo() is null");
        netInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
    }

    if (netInfo == null || netInfo.isAvailable() == false) {
        DebugLog.d(TAG, "getCurNetworkInfo() netInfo is null line1383");
        if (mbNetworIsConnect != false) {
            mbNetworIsConnect = false;
            reloadUrl = GameInfo.getErrorUrl();
            reload(reloadUrl);
        }
        Utils.setNetworkType(Constants.NETWORKTYPE_NONE);
        return;
    }
    netType = netInfo.getType();
    netSubtype = netInfo.getSubtype();
    isConnected = netInfo.isConnected();
    DebugLog.e(TAG, "======processNetworkType nettype = " + netType + ",netSubtype = " + netSubtype
            + ",isConnect = " + isConnected);
    switch (netType) {
    case ConnectivityManager.TYPE_MOBILE_SUPL:
    case ConnectivityManager.TYPE_MOBILE_MMS:
    case ConnectivityManager.TYPE_MOBILE_HIPRI:
    case ConnectivityManager.TYPE_MOBILE_DUN:
    case ConnectivityManager.TYPE_MOBILE:
        DebugLog.d(TAG, "processNetworkType ConnectivityManager.TYPE_MOBILE netSubtype=" + netSubtype);
        if (netSubtype == TelephonyManager.NETWORK_TYPE_LTE) {//4
            DebugLog.d(TAG, "processNetworkType ConnectivityManager.TYPE_MOBILE 4G");
            networkType = Constants.NETWORKTYPE_LTE;
        } else {//3
            DebugLog.d(TAG, "processNetworkType ConnectivityManager.TYPE_MOBILE 3G");
            networkType = Constants.NETWORKTYPE_3G;
        }
        break;
    case ConnectivityManager.TYPE_WIFI:
        DebugLog.d(TAG, "processNetworkType ConnectivityManager.TYPE_WIFI");
        networkType = Constants.NETWORKTYPE_WIFI;
        break;
    case ConnectivityManager.TYPE_ETHERNET:
        DebugLog.d(TAG, "processNetworkType ConnectivityManager.TYPE_ETHERNET");
        networkType = Constants.NETWORKTYPE_ETHERNET;
        break;
    default:
        DebugLog.d(TAG, "other network status");
        networkType = Constants.NETWORKTYPE_OTHERS;
        break;
    }
    DebugLog.e(TAG, "======processNetworkType nettype = " + netType + ",netSubtype = " + netSubtype
            + ",isConnect = " + isConnected + ",mbNetworIsConnect = " + mbNetworIsConnect);
    if (isConnected) {
        reloadUrl = Utils.checkUrl(GameInfo.DEFAULT_URL);
        Utils.setNetworkType(networkType);
        //         Utils.setNetworkType(Constants.NETWORKTYPE_WIFI);//temp change
    } else {
        reloadUrl = GameInfo.getErrorUrl();
        Utils.setNetworkType(Constants.NETWORKTYPE_NONE);
    }

    //      if((networkType == Constants.NETWORKTYPE_WIFI || networkType == Constants.NETWORKTYPE_LTE) && mbNetworIsConnect != isConnected){
    if (mbNetworIsConnect != isConnected) {
        mbNetworIsConnect = isConnected;
        reload(reloadUrl);
        DebugLog.d(TAG, "Notify reload finish");
    }
}