Java Code Examples for android.net.ConnectivityManager#TYPE_MOBILE

The following examples show how to use android.net.ConnectivityManager#TYPE_MOBILE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SPV.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
private void setSyncOnMobileEnabled(final boolean enabled) {
    synchronized (mStateLock) {
        final boolean current = isSyncOnMobileEnabled();
        final boolean currentlyEnabled = isEnabled();
        Log.d(TAG, "setSyncOnMobileEnabled: " + Var("enabled", enabled) + Var("current", current));
        if (enabled == current)
            return; // Setting hasn't changed

        mService.cfgEdit("SPV").putBoolean("mobileSyncEnabled", enabled).apply();

        if (getNetworkType() != ConnectivityManager.TYPE_MOBILE)
            return; // Any change doesn't affect us since we aren't currently on mobile

        if (enabled && currentlyEnabled) {
            if (mPeerGroup == null)
                setup();
            startSync();
        }
        else
            stopSync();
    }
}
 
Example 2
Source File: SystemConfig.java    From AndroidDownload with Apache License 2.0 6 votes vote down vote up
public static int getNetType(){
    ConnectivityManager connectivityManager = (ConnectivityManager) x.app().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager == null) {
        return Const.NET_TYPE_UNKNOW;
    }
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetworkInfo == null) {
        return Const.NET_TYPE_UNKNOW;
    }
    int type = activeNetworkInfo.getType();
    if(type==ConnectivityManager.TYPE_WIFI){
        return Const.NET_TYPE_WIFI;
    }else  if(type==ConnectivityManager.TYPE_MOBILE){
        return Const.NET_TYPE_MOBILE;
    }
    return Const.NET_TYPE_UNKNOW;
}
 
Example 3
Source File: Preferences.java    From fdroidclient with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Some users never use WiFi, this lets us check for that state on first run.
 */
public void setDefaultForDataOnlyConnection(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null) {
        return;
    }
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork == null || !activeNetwork.isConnectedOrConnecting()) {
        return;
    }
    if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
        NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (!wifiNetwork.isConnectedOrConnecting()) {
            preferences.edit().putInt(PREF_OVER_DATA, OVER_NETWORK_ALWAYS).apply();
        }
    }
}
 
Example 4
Source File: StorageAndControlService.java    From boilr with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Checks the network connection and sets the wifiConnected
 * and mobileConnected variables accordingly.
 */
private void updateConnectedFlags() {
	ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
	NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
	if(activeInfo != null && activeInfo.isConnected()) {
		wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
		mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
	} else {
		wifiConnected = false;
		mobileConnected = false;
	}
}
 
Example 5
Source File: NetworkUtils.java    From ans-android-sdk with GNU General Public License v3.0 5 votes vote down vote up
/**
 *  获取网络类型
  * @param context
 * @param flag  true获取详细的网络运营商类型,false只获取运营商
 * @return
 */
public static String networkType(Context context,boolean flag) {
    String netType = "";
    // 检测权限
    if (!CommonUtils.checkPermission(context, Manifest.permission.ACCESS_NETWORK_STATE)) {
        return "Unknown";
    }
    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context
            .CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = null;
    if (connMgr != null) {
        networkInfo = connMgr.getActiveNetworkInfo();
    }

    if (networkInfo == null) {
        return netType;
    }
    int nType = networkInfo.getType();
    if (nType == ConnectivityManager.TYPE_WIFI) {
        netType = "WIFI";
    } else if (nType == ConnectivityManager.TYPE_MOBILE) {
        if(flag){
            int nSubType = networkInfo.getSubtype();
            return String.valueOf(nSubType);
        } else {
            //返回移动网络
            return "WWAN";
        }
    }
    return netType;
}
 
Example 6
Source File: NetworkUtil.java    From landlord_client with Apache License 2.0 5 votes vote down vote up
/**
 * 获取网络连接类型
 * <p/>
 * GPRS    2G(2.5) General Packet Radia Service 114kbps
 * EDGE    2G(2.75G) Enhanced Data Rate for GSM Evolution 384kbps
 * UMTS    3G WCDMA 联通3G Universal Mobile Telecommunication System 完整的3G移动通信技术标准
 * CDMA    2G 电信 Code Division Multiple Access 码分多址
 * EVDO_0  3G (EVDO 全程 CDMA2000 1xEV-DO) Evolution - Data Only (Data Optimized) 153.6kps - 2.4mbps 属于3G
 * EVDO_A  3G 1.8mbps - 3.1mbps 属于3G过渡,3.5G
 * 1xRTT   2G CDMA2000 1xRTT (RTT - 无线电传输技术) 144kbps 2G的过渡,
 * HSDPA   3.5G 高速下行分组接入 3.5G WCDMA High Speed Downlink Packet Access 14.4mbps
 * HSUPA   3.5G High Speed Uplink Packet Access 高速上行链路分组接入 1.4 - 5.8 mbps
 * HSPA    3G (分HSDPA,HSUPA) High Speed Packet Access
 * IDEN    2G Integrated Dispatch Enhanced Networks 集成数字增强型网络 (属于2G,来自维基百科)
 * EVDO_B  3G EV-DO Rev.B 14.7Mbps 下行 3.5G
 * LTE     4G Long Term Evolution FDD-LTE 和 TDD-LTE , 3G过渡,升级版 LTE Advanced 才是4G
 * EHRPD   3G CDMA2000向LTE 4G的中间产物 Evolved High Rate Packet Data HRPD的升级
 * HSPAP   3G HSPAP 比 HSDPA 快些
 *
 * @return {@link  NetWorkType}
 */
public static NetWorkType getNetworkType(Context context) {
    int type = getConnectedTypeINT(context);
    switch (type) {
        case ConnectivityManager.TYPE_WIFI:
            return NetWorkType.Wifi;
        case ConnectivityManager.TYPE_MOBILE:
        case ConnectivityManager.TYPE_MOBILE_DUN:
        case ConnectivityManager.TYPE_MOBILE_HIPRI:
        case ConnectivityManager.TYPE_MOBILE_MMS:
        case ConnectivityManager.TYPE_MOBILE_SUPL:
            int teleType = getTelephonyManager(context).getNetworkType();
            switch (teleType) {
                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case TelephonyManager.NETWORK_TYPE_IDEN:
                    return NetWorkType.Net2G;
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case TelephonyManager.NETWORK_TYPE_EVDO_B:
                case TelephonyManager.NETWORK_TYPE_EHRPD:
                case TelephonyManager.NETWORK_TYPE_HSPAP:
                    return NetWorkType.Net3G;
                case TelephonyManager.NETWORK_TYPE_LTE:
                    return NetWorkType.Net4G;
                default:
                    return NetWorkType.UnKnown;
            }
        default:
            return NetWorkType.UnKnown;
    }
}
 
Example 7
Source File: WifiDelegate.java    From wifi with MIT License 5 votes vote down vote up
private void launchIP() {
    NetworkInfo info = ((ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    if (info != null && info.isConnected()) {
        if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            try {
                for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                    NetworkInterface intf = en.nextElement();
                    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                            result.success(inetAddress.getHostAddress());
                            clearMethodCallAndResult();
                        }
                    }
                }
            } catch (SocketException e) {
                e.printStackTrace();
            }
        } else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            String ipAddress = intIP2StringIP(wifiInfo.getIpAddress());
            result.success(ipAddress);
            clearMethodCallAndResult();
        }
    } else {
        finishWithError("unavailable", "ip not available.");
    }
}
 
Example 8
Source File: NetworkUtils.java    From CrawlerForReader with Apache License 2.0 5 votes vote down vote up
/**
 * 判断网络是否是移动数据
 * <p>需添加权限
 * {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />}</p>
 *
 * @return {@code true}: 是<br>{@code false}: 否
 */
@SuppressLint("MissingPermission")
public static boolean isMobileData() {
    NetworkInfo info = getActiveNetworkInfo();
    return null != info
            && info.isAvailable()
            && info.getType() == ConnectivityManager.TYPE_MOBILE;
}
 
Example 9
Source File: PicassoExecutorService.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
void adjustThreadCount(NetworkInfo info) {
  if (info == null || !info.isConnectedOrConnecting()) {
    setThreadCount(DEFAULT_THREAD_COUNT);
    return;
  }
  switch (info.getType()) {
    case ConnectivityManager.TYPE_WIFI:
    case ConnectivityManager.TYPE_WIMAX:
    case ConnectivityManager.TYPE_ETHERNET:
      setThreadCount(4);
      break;
    case ConnectivityManager.TYPE_MOBILE:
      switch (info.getSubtype()) {
        case TelephonyManager.NETWORK_TYPE_LTE:  // 4G
        case TelephonyManager.NETWORK_TYPE_HSPAP:
        case TelephonyManager.NETWORK_TYPE_EHRPD:
          setThreadCount(3);
          break;
        case TelephonyManager.NETWORK_TYPE_UMTS: // 3G
        case TelephonyManager.NETWORK_TYPE_CDMA:
        case TelephonyManager.NETWORK_TYPE_EVDO_0:
        case TelephonyManager.NETWORK_TYPE_EVDO_A:
        case TelephonyManager.NETWORK_TYPE_EVDO_B:
          setThreadCount(2);
          break;
        case TelephonyManager.NETWORK_TYPE_GPRS: // 2G
        case TelephonyManager.NETWORK_TYPE_EDGE:
          setThreadCount(1);
          break;
        default:
          setThreadCount(DEFAULT_THREAD_COUNT);
      }
      break;
    default:
      setThreadCount(DEFAULT_THREAD_COUNT);
  }
}
 
Example 10
Source File: VoIPBaseService.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
protected int getStatsNetworkType() {
	int netType = StatsController.TYPE_WIFI;
	if (lastNetInfo != null) {
		if (lastNetInfo.getType() == ConnectivityManager.TYPE_MOBILE)
			netType = lastNetInfo.isRoaming() ? StatsController.TYPE_ROAMING : StatsController.TYPE_MOBILE;
	}
	return netType;
}
 
Example 11
Source File: AVConnectivityReceiver.java    From java-unified-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
  if (null == this.listener || null == context) {
    return;
  }
  int hasPermission = ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_NETWORK_STATE);
  if (PackageManager.PERMISSION_GRANTED != hasPermission) {
    LogUtil.getLogger(AVConnectivityReceiver.class).w("android.Manifest.permission.ACCESS_NETWORK_STATE is not granted.");
    return;
  }

  ConnectivityManager cm =
      (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

  try {
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (null == activeNetwork || !activeNetwork.isConnected()) {
      this.listener.onNotConnected(context);
      connectivityBroken = true;
      return;
    }
    connectivityBroken = false;
    if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
      this.listener.onMobile(context);
    } else if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
      this.listener.onWifi(context);
    } else {
      this.listener.onOtherConnected(context);
    }
  } catch (Exception ex) {
    ;
  }
}
 
Example 12
Source File: DeviceHelper.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
/**
	 * get current network type, ps:3G��2G
	 * 
	 * @return String
	 * */
	public String getNetworkType() {
		ConnectivityManager conn = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		if (conn == null) {
			return "none";
		}

		NetworkInfo network = conn.getActiveNetworkInfo();
		if (network == null || !network.isAvailable()) {
			return "none";
		}

		int type = network.getType();
		if (ConnectivityManager.TYPE_WIFI == type) {
			return "wifi";
		}

		if (ConnectivityManager.TYPE_MOBILE == type) {
//			String proxy = Proxy.getDefaultHost();
			String wap = "";
//			if (proxy != null && proxy.length() > 0) {
//				wap = " wap";
//			}

			return (isFastMobileNetwork() ? "3G" : "2G") + wap;
		}

		return "none";
	}
 
Example 13
Source File: NetworkUtil.java    From ViewDebugHelper with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static boolean isMobileNetworkActive(Context context) {
	ConnectivityManager connectivity = (ConnectivityManager) context
			.getSystemService(Context.CONNECTIVITY_SERVICE);
	if (connectivity == null) {
		return false;
	}

	final NetworkInfo activeInfo = connectivity.getActiveNetworkInfo();
	if (activeInfo == null) {
		return false;
	} else if (activeInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
		return true;
	}
	return false;
}
 
Example 14
Source File: NetworkUtils.java    From pandroid with Apache License 2.0 4 votes vote down vote up
/**
 * Check if the connection is fast
 *
 * @param type
 * @param subType
 * @return
 */
public static boolean isConnectionFast(int type, int subType) {
    if (type == ConnectivityManager.TYPE_WIFI) {
        return true;
    } else if (type == ConnectivityManager.TYPE_MOBILE) {
        switch (subType) {
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                return false; // ~ 50-100 kbps
            case TelephonyManager.NETWORK_TYPE_CDMA:
                return false; // ~ 14-64 kbps
            case TelephonyManager.NETWORK_TYPE_EDGE:
                return false; // ~ 50-100 kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                return true; // ~ 400-1000 kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
                return true; // ~ 600-1400 kbps
            case TelephonyManager.NETWORK_TYPE_GPRS:
                return false; // ~ 100 kbps
            case TelephonyManager.NETWORK_TYPE_HSDPA:
                return true; // ~ 2-14 Mbps
            case TelephonyManager.NETWORK_TYPE_HSPA:
                return true; // ~ 700-1700 kbps
            case TelephonyManager.NETWORK_TYPE_HSUPA:
                return true; // ~ 1-23 Mbps
            case TelephonyManager.NETWORK_TYPE_UMTS:
                return true; // ~ 400-7000 kbps
        /*
         * Above API level 7, make sure to set android:targetSdkVersion
* to appropriate level to use these
*/
            case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
                return true; // ~ 1-2 Mbps
            case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
                return true; // ~ 5 Mbps
            case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
                return true; // ~ 10-20 Mbps
            case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
                return false; // ~25 kbps
            case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
                return true; // ~ 10+ Mbps
            // Unknown
            case TelephonyManager.NETWORK_TYPE_UNKNOWN:
            default:
                return false;
        }
    } else {
        return false;
    }
}
 
Example 15
Source File: LocalNetHelper.java    From base-module with Apache License 2.0 4 votes vote down vote up
/**
 * 获取当前的网络类型(WIFI,2G,3G,4G)
 * <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
 *
 * @param context 上下文
 * @return 网络类型
 * <ul>
 * <li>{@link #NETWORK_WIFI   } = 1;</li>
 * <li>{@link #NETWORK_4G     } = 4;</li>
 * <li>{@link #NETWORK_3G     } = 3;</li>
 * <li>{@link #NETWORK_2G     } = 2;</li>
 * <li>{@link #NETWORK_UNKNOWN} = 5;</li>
 * <li>{@link #NETWORK_NO     } = -1;</li>
 * </ul>
 */
public static int getNetWorkType(Context context) {
    int netType = NETWORK_NO;
    NetworkInfo info = getActiveNetworkInfo(context);
    if (info != null && info.isAvailable()) {

        if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            netType = NETWORK_WIFI;
        } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            switch (info.getSubtype()) {

                case NETWORK_TYPE_GSM:
                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case TelephonyManager.NETWORK_TYPE_IDEN:
                    netType = NETWORK_2G;
                    break;

                case NETWORK_TYPE_TD_SCDMA:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case TelephonyManager.NETWORK_TYPE_EVDO_B:
                case TelephonyManager.NETWORK_TYPE_EHRPD:
                case TelephonyManager.NETWORK_TYPE_HSPAP:
                    netType = NETWORK_3G;
                    break;

                case NETWORK_TYPE_IWLAN:
                case TelephonyManager.NETWORK_TYPE_LTE:
                    netType = NETWORK_4G;
                    break;
                default:

                    String subtypeName = info.getSubtypeName();
                    if (subtypeName.equalsIgnoreCase("TD-SCDMA")
                            || subtypeName.equalsIgnoreCase("WCDMA")
                            || subtypeName.equalsIgnoreCase("CDMA2000")) {
                        netType = NETWORK_3G;
                    } else {
                        netType = NETWORK_UNKNOWN;
                    }
                    break;
            }
        } else {
            netType = NETWORK_UNKNOWN;
        }
    }
    return netType;
}
 
Example 16
Source File: CheckConnection.java    From wmn-safety with MIT License 4 votes vote down vote up
public Boolean isMobileConnected(){
    NetworkInfo networkInfo = getNetworkInfo();
    return networkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
}
 
Example 17
Source File: Connectivity.java    From hipda with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Check if the connection is fast
 */
public static boolean isConnectionFast(int type, int subType) {
    if (type == ConnectivityManager.TYPE_WIFI) {
        return true;
    } else if (type == ConnectivityManager.TYPE_MOBILE) {
        switch (subType) {
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                return false; // ~ 50-100 kbps
            case TelephonyManager.NETWORK_TYPE_CDMA:
                return false; // ~ 14-64 kbps
            case TelephonyManager.NETWORK_TYPE_EDGE:
                return false; // ~ 50-100 kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                return true; // ~ 400-1000 kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
                return true; // ~ 600-1400 kbps
            case TelephonyManager.NETWORK_TYPE_GPRS:
                return false; // ~ 100 kbps
            case TelephonyManager.NETWORK_TYPE_HSDPA:
                return true; // ~ 2-14 Mbps
            case TelephonyManager.NETWORK_TYPE_HSPA:
                return true; // ~ 700-1700 kbps
            case TelephonyManager.NETWORK_TYPE_HSUPA:
                return true; // ~ 1-23 Mbps
            case TelephonyManager.NETWORK_TYPE_UMTS:
                return true; // ~ 400-7000 kbps
        /*
         * Above API level 7, make sure to set android:targetSdkVersion
         * to appropriate level to use these
         */
            case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
                return true; // ~ 1-2 Mbps
            case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
                return true; // ~ 5 Mbps
            case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
                return true; // ~ 10-20 Mbps
            case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
                return false; // ~25 kbps
            case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
                return true; // ~ 10+ Mbps
            // Unknown
            case TelephonyManager.NETWORK_TYPE_UNKNOWN:
            default:
                return false;
        }
    } else {
        return false;
    }
}
 
Example 18
Source File: NetworkConnectChangedReceiver.java    From apollo-DuerOS with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    // Monitor wifi status, have nothing to do with wifi connection
    if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
        LogUtil.d(TAG, "wifiState:" + wifiState);
        switch (wifiState) {
            case WifiManager.WIFI_STATE_DISABLED:
                break;
            case WifiManager.WIFI_STATE_DISABLING:
                break;
            default:
                break;
        }
    }
    // Monitor the connection status of WiFi, that is whether or not there is an effective wireless routing
    if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        Parcelable parcelableExtra = intent
                .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if (null != parcelableExtra) {
            // Get the network status of the NetWorkInfo object
            NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
            // The State object is the state of connection success or not
            NetworkInfo.State state = networkInfo.getState();
            // Determine whether or not the network has been connected
            boolean isConnected = state == NetworkInfo.State.CONNECTED;
            LogUtil.d(TAG, "isConnected:" + isConnected);
        }
    }
    // To monitor the network connection, including wifi and mobile data opening and closing,
    // as well as connection available on the connection had been listening
    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
        // Get the NetworkInfo object of the network state
        NetworkInfo info = intent
                .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        if (info != null) {
            // If the current network connection is successful and the network connection is available
            if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) {
                if (info.getType() == ConnectivityManager.TYPE_WIFI
                        || info.getType() == ConnectivityManager.TYPE_MOBILE) {
                    LogUtil.i(TAG, getConnectionType(info.getType()) + " connected");
                    // transform to the following module
                    if (mOnNetworkChangeListener != null) {
                        LogUtil.i(TAG, "mOnLocationListener is set");
                        mOnNetworkChangeListener.onNetworkChange();
                    }
                }
            } else {
                LogUtil.i(TAG, getConnectionType(info.getType()) + " disconnected");
                if (mOnNetworkChangeListener != null) {
                    LogUtil.i(TAG, "onShowCompass is set");
                    mOnNetworkChangeListener.onShowCompass();
                }
            }
        }

    }
}
 
Example 19
Source File: DataCollector.java    From evercam-android with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Check if there is any connectivity to a mobile network
 */
public boolean isConnectedMobile() {
    NetworkInfo info = getNetworkInfo();
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager
            .TYPE_MOBILE);
}
 
Example 20
Source File: NetConnectedUtils.java    From OpenEyes with Apache License 2.0 2 votes vote down vote up
/**
 * 2.判断手机网络是否存在
 *
 * @param context 上下文
 * @return 返回 false 无手机网络   true 有手机网络
 */
public static boolean isPhoneNetConnected(Context context) {
    int typeMobile = ConnectivityManager.TYPE_MOBILE;//手机网络类型
    return isNetworkConnected(context, typeMobile);
}