Search in sources :

Example 91 with NetworkInfo

use of android.net.NetworkInfo in project SimplifyReader by chentao0707.

the class NetUtils method getConnectedType.

public static int getConnectedType(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
            return mNetworkInfo.getType();
        }
    }
    return -1;
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager)

Example 92 with NetworkInfo

use of android.net.NetworkInfo in project SimplifyReader by chentao0707.

the class NetUtils method isMobileConnected.

public static boolean isMobileConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mMobileNetworkInfo != null) {
            return mMobileNetworkInfo.isAvailable();
        }
    }
    return false;
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager)

Example 93 with NetworkInfo

use of android.net.NetworkInfo in project SimplifyReader by chentao0707.

the class NetUtils method getAPNType.

public static NetType getAPNType(Context context) {
    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo == null) {
        return NetType.NONE;
    }
    int nType = networkInfo.getType();
    if (nType == ConnectivityManager.TYPE_MOBILE) {
        if (networkInfo.getExtraInfo().toLowerCase(Locale.getDefault()).equals("cmnet")) {
            return NetType.CMNET;
        } else {
            return NetType.CMWAP;
        }
    } else if (nType == ConnectivityManager.TYPE_WIFI) {
        return NetType.WIFI;
    }
    return NetType.NONE;
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager)

Example 94 with NetworkInfo

use of android.net.NetworkInfo in project android by owncloud.

the class ConnectivityActionReceiver method onReceive.

@Override
public void onReceive(final Context context, Intent intent) {
    // LOG ALL EVENTS:
    Log_OC.v(TAG, "action: " + intent.getAction());
    Log_OC.v(TAG, "component: " + intent.getComponent());
    Bundle extras = intent.getExtras();
    if (extras != null) {
        for (String key : extras.keySet()) {
            Log_OC.v(TAG, "key [" + key + "]: " + extras.get(key));
        }
    } else {
        Log_OC.v(TAG, "no extras");
    }
    /**
         * There is an interesting mess to process WifiManager.NETWORK_STATE_CHANGED_ACTION and
         * ConnectivityManager.CONNECTIVITY_ACTION in a simple and reliable way.
         *
         * The former triggers much more events than what we really need to know about Wifi connection.
         *
         * But there are annoying uncertainties about ConnectivityManager.CONNECTIVITY_ACTION due
         * to the deprecation of ConnectivityManager.EXTRA_NETWORK_INFO in API level 14, and the absence
         * of ConnectivityManager.EXTRA_NETWORK_TYPE until API level 17. Dear Google, how should we
         * handle API levels 14 to 16?
         *
         * In the end maybe we need to keep in memory the current knowledge about connectivity
         * and update it taking into account several Intents received in a row
         *
         * But first let's try something "simple" to keep a basic retry of instant uploads in
         * version 1.9.2, similar to the existent until 1.9.1. To be improved.
         */
    if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
        NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        WifiInfo wifiInfo = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
        String bssid = intent.getStringExtra(WifiManager.EXTRA_BSSID);
        if (// not enough; see (*) right below
        networkInfo.isConnected() && wifiInfo != null && !UNKNOWN_SSID.equals(wifiInfo.getSSID().toLowerCase()) && bssid != null) {
            Log_OC.d(TAG, "WiFi connected");
            wifiConnected(context);
        } else {
            // TODO tons of things to check to conclude disconnection;
            // TODO maybe alternative commented below, based on CONNECTIVITY_ACTION is better
            Log_OC.d(TAG, "WiFi disconnected ... but don't know if right now");
        }
    }
// (*) When WiFi is lost, an Intent with network state CONNECTED and SSID "<unknown ssid>" is
//      received right before another Intent with network state DISCONNECTED; needs to
//      be differentiated of a new Wifi connection.
//
//  Besides, with a new connection two Intents are received, having only the second the extra
//  WifiManager.EXTRA_BSSID, with the BSSID of the access point accessed.
//
//  Not sure if this protocol is exact, since it's not documented. Only found mild references in
//   - http://developer.android.com/intl/es/reference/android/net/wifi/WifiInfo.html#getSSID()
//   - http://developer.android.com/intl/es/reference/android/net/wifi/WifiManager.html#EXTRA_BSSID
//  and reproduced in Nexus 5 with Android 6.
/**
         * Possible alternative attending ConnectivityManager.CONNECTIVITY_ACTION.
         *
         * Let's see what QA has to say
         *
        if(intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            NetworkInfo networkInfo = intent.getParcelableExtra(
                    ConnectivityManager.EXTRA_NETWORK_INFO      // deprecated in API 14
            );
            int networkType = intent.getIntExtra(
                    ConnectivityManager.EXTRA_NETWORK_TYPE,     // only from API level 17
                    -1
            );
            boolean couldBeWifiAction =
                    (networkInfo == null && networkType < 0)    ||      // cases of lack of info
                    networkInfo.getType() == ConnectivityManager.TYPE_WIFI  ||
                    networkType == ConnectivityManager.TYPE_WIFI;

            if (couldBeWifiAction) {
                if (ConnectivityUtils.isAppConnectedViaWiFi(context)) {
                    Log_OC.d(TAG, "WiFi connected");
                    wifiConnected(context);
                } else {
                    Log_OC.d(TAG, "WiFi disconnected");
                    wifiDisconnected(context);
                }
            } /* else, CONNECTIVIY_ACTION is (probably) about other network interface (mobile, bluetooth, ...)
        }
        */
}
Also used : NetworkInfo(android.net.NetworkInfo) Bundle(android.os.Bundle) WifiInfo(android.net.wifi.WifiInfo)

Example 95 with NetworkInfo

use of android.net.NetworkInfo in project android-priority-jobqueue by path.

the class NetworkUtilImpl method isConnected.

@Override
public boolean isConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager)

Aggregations

NetworkInfo (android.net.NetworkInfo)511 ConnectivityManager (android.net.ConnectivityManager)274 Intent (android.content.Intent)47 LinkProperties (android.net.LinkProperties)38 NetworkState (android.net.NetworkState)26 Test (org.junit.Test)26 PendingIntent (android.app.PendingIntent)23 NetworkCapabilities (android.net.NetworkCapabilities)18 Network (android.net.Network)17 LargeTest (android.test.suitebuilder.annotation.LargeTest)17 RemoteException (android.os.RemoteException)16 Context (android.content.Context)15 NetworkAgentInfo (com.android.server.connectivity.NetworkAgentInfo)15 WifiInfo (android.net.wifi.WifiInfo)13 Bundle (android.os.Bundle)13 TestUtils.mockNetworkInfo (com.squareup.picasso.TestUtils.mockNetworkInfo)12 IOException (java.io.IOException)11 WifiManager (android.net.wifi.WifiManager)9 IntentFilter (android.content.IntentFilter)8 BroadcastReceiver (android.content.BroadcastReceiver)7