Java Code Examples for android.net.NetworkInfo#getType()

The following examples show how to use android.net.NetworkInfo#getType() . 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: ConnectivityMonitor.java    From unity-ads-android with Apache License 2.0 6 votes vote down vote up
private static void initConnectionStatus() {
	ConnectivityManager cm = (ConnectivityManager)ClientProperties.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

	if(cm == null) {
		return;
	}

	NetworkInfo ni  = cm.getActiveNetworkInfo();

	if(ni != null && ni.isConnected()) {
		_connected = 1;
		_wifi = ni.getType() == ConnectivityManager.TYPE_WIFI;

		if(!_wifi) {
			TelephonyManager tm = (TelephonyManager)ClientProperties.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
			_networkType = tm.getNetworkType();
		}
	} else {
		_connected = 0;
	}
}
 
Example 2
Source File: ConnectivityManagerCompat.java    From V.FlyoutTest with MIT License 6 votes vote down vote up
@Override
public boolean isActiveNetworkMetered(ConnectivityManager cm) {
    final NetworkInfo info = cm.getActiveNetworkInfo();
    if (info == null) {
        // err on side of caution
        return true;
    }

    final int type = info.getType();
    switch (type) {
        case TYPE_MOBILE:
            return true;
        case TYPE_WIFI:
            return false;
        default:
            // err on side of caution
            return true;
    }
}
 
Example 3
Source File: SelectLocationTabsActivity.java    From MuslimMateAndroid with GNU General Public License v3.0 6 votes vote down vote up
public static boolean isInternetOn(Context context) {
    boolean isMobile = false, isWifi = false;
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] infoAvailableNetworks = cm.getAllNetworkInfo();

    if (infoAvailableNetworks != null) {
        for (NetworkInfo network : infoAvailableNetworks) {

            if (network.getType() == ConnectivityManager.TYPE_WIFI) {
                if (network.isConnected() && network.isAvailable())
                    isWifi = true;
            }
            if (network.getType() == ConnectivityManager.TYPE_MOBILE) {
                if (network.isConnected() && network.isAvailable())
                    isMobile = true;
            }
        }
    }

    return isMobile || isWifi;

}
 
Example 4
Source File: SystemFacade.java    From UnityOBBDownloader with Apache License 2.0 6 votes vote down vote up
public Integer getActiveNetworkType() {
    ConnectivityManager connectivity =
            (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        Log.w(Constants.TAG, "couldn't get connectivity manager");
        return null;
    }

    NetworkInfo activeInfo = connectivity.getActiveNetworkInfo();
    if (activeInfo == null) {
        if (Constants.LOGVV) {
            Log.v(Constants.TAG, "network is not available");
        }
        return null;
    }
    return activeInfo.getType();
}
 
Example 5
Source File: NetUtil.java    From ZhihuDailyFluxRRD with Apache License 2.0 5 votes vote down vote up
public static int getConnectedType() {
    if (AppContextUtil.instance() != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) AppContextUtil.instance()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
            return mNetworkInfo.getType();
        }
    }
    return -1;
}
 
Example 6
Source File: Network.java    From PicKing with Apache License 2.0 5 votes vote down vote up
/**
 * 判断是否有可用状态的Wifi,以下情况返回false:
 * 1. 设备wifi开关关掉;
 * 2. 已经打开飞行模式;
 * 3. 设备所在区域没有信号覆盖;
 * 4. 设备在漫游区域,且关闭了网络漫游。
 *
 * @return boolean wifi为可用状态(不一定成功连接,即Connected)即返回ture
 */
public static boolean isWifiAvailable(Context context) {
    NetworkInfo[] nets = getConnectivityManager(context).getAllNetworkInfo();
    if (nets != null) {
        for (NetworkInfo net : nets) {
            if (net.getType() == ConnectivityManager.TYPE_WIFI) { return net.isAvailable(); }
        }
    }
    return false;
}
 
Example 7
Source File: ConnectivityHelper.java    From ratebeer with GNU General Public License v3.0 5 votes vote down vote up
public static ConnectivityType current(Context context) {
	ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	NetworkInfo ni = cm.getActiveNetworkInfo();
	if (ni == null || !ni.isConnectedOrConnecting())
		return ConnectivityType.NoConnection;
	if (ni.getType() == ConnectivityManager.TYPE_WIFI || ni.getType() == ConnectivityManager.TYPE_ETHERNET)
		return ConnectivityType.Wifi;
	// Return Cellular for any other type, including mobile, vpn and wimax
	return ConnectivityType.Cellular;
}
 
Example 8
Source File: NetWorkUtils.java    From youqu_master with Apache License 2.0 5 votes vote down vote up
/**
 * 检测3G是否连接
 */
public static boolean is3gConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm != null) {
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
            return true;
        }
    }
    return false;
}
 
Example 9
Source File: ConnectivityActionReceiver.java    From AntennaPodSP with MIT License 5 votes vote down vote up
@Override
public void onReceive(final Context context, Intent intent) {
    if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
        if (AppConfig.DEBUG)
            Log.d(TAG, "Received intent");

        if (NetworkUtils.autodownloadNetworkAvailable(context)) {
            /*
            if (AppConfig.DEBUG)
                Log.d(TAG,
                        "auto-dl network available, starting auto-download");
            DBTasks.autodownloadUndownloadedItems(context);
            */
        } else { // if new network is Wi-Fi, finish ongoing downloads,
            // otherwise cancel all downloads
            ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo ni = cm.getActiveNetworkInfo();
            if (ni == null || ni.getType() != ConnectivityManager.TYPE_WIFI) {
                if (AppConfig.DEBUG)
                    Log.i(TAG,
                            "Device is no longer connected to Wi-Fi. Cancelling ongoing downloads");
                DownloadRequester.getInstance().cancelAllDownloads(context);
            }

        }
    }
}
 
Example 10
Source File: NotificationConnectionHandler.java    From an2linuxclient with GNU General Public License v3.0 5 votes vote down vote up
static void sendToAllEnabledMobileServers(Notification n, Context c, List<MobileServer> enabledMobileServers){
    ConnectivityManager connMgr = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected() && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
        for(MobileServer mobileServer : enabledMobileServers){
            if (!networkInfo.isRoaming() || mobileServer.isRoamingAllowed()){
                ThreadPoolHandler.enqueueRunnable(new TcpNotificationConnection(c, n,
                        mobileServer.getCertificate(),
                        mobileServer.getIpOrHostname(),
                        mobileServer.getPortNumber()));
            }
        }
    }
}
 
Example 11
Source File: ConnectionUtils.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
public static boolean isConnectedToWifi(Context context) {

        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        return ni != null && ni.isConnected()
                && ni.getType() == ConnectivityManager.TYPE_WIFI;
    }
 
Example 12
Source File: Connectivity.java    From matomo-sdk-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Type getType() {
    NetworkInfo network = mConnectivityManager.getActiveNetworkInfo();
    if (network == null) return NONE;
    if (network.getType() == ConnectivityManager.TYPE_WIFI) {
        return WIFI;
    } else return MOBILE;
}
 
Example 13
Source File: NetUtils.java    From NoHttp with Apache License 2.0 5 votes vote down vote up
private static boolean isConnected(NetType netType, NetworkInfo networkInfo) {
    if (networkInfo == null) return false;

    switch (netType) {
        case Wifi: {
            if (!isConnected(networkInfo)) return false;
            return networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
        }
        case Wired: {
            if (!isConnected(networkInfo)) return false;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
                return networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET;
            return false;
        }
        case Mobile: {
            if (!isConnected(networkInfo)) return false;
            return networkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
        }
        case Mobile2G: {
            if (!isConnected(Mobile, networkInfo)) return false;
            return isMobileSubType(Mobile2G, networkInfo);
        }
        case Mobile3G: {
            if (!isConnected(Mobile, networkInfo)) return false;
            return isMobileSubType(Mobile3G, networkInfo);
        }
        case Mobile4G: {
            if (!isConnected(Mobile, networkInfo)) return false;
            return isMobileSubType(Mobile4G, networkInfo);
        }
    }
    return false;
}
 
Example 14
Source File: Utils.java    From Android-HttpDownloadManager with Apache License 2.0 5 votes vote down vote up
/**
 * To check whether current network is wifi.
 *
 * @param context context
 * @return true if network if wifi, otherwise return false
 */
static boolean isWifi(Context context) {
  if (context == null) {
    return false;
  }

  ConnectivityManager manager =
      (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  if (manager == null) {
    return false;
  }
  NetworkInfo info = manager.getActiveNetworkInfo();

  return info != null && (info.getType() == ConnectivityManager.TYPE_WIFI);
}
 
Example 15
Source File: CheckConnection.java    From wmn-safety with MIT License 4 votes vote down vote up
public Boolean isWifiConnected(){
    NetworkInfo networkInfo = getNetworkInfo();
    return networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
}
 
Example 16
Source File: SPV.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
private int getNetworkType(final NetworkInfo info) {
    if (info == null)
        return ConnectivityManager.TYPE_DUMMY;
    final int type = info.getType();
    return type == ConnectivityManager.TYPE_MOBILE ? type : ConnectivityManager.TYPE_ETHERNET;
}
 
Example 17
Source File: NetworkUtils.java    From Android-utils with Apache License 2.0 4 votes vote down vote up
@RequiresPermission(ACCESS_NETWORK_STATE)
public static boolean isMobileData() {
    NetworkInfo info = getActiveNetworkInfo();
    return null != info && info.isAvailable() && info.getType() == ConnectivityManager.TYPE_MOBILE;
}
 
Example 18
Source File: NetCompat.java    From DMusic with Apache License 2.0 4 votes vote down vote up
/**
 * Get the current network type
 *
 * @return type 0: NO_AVAILABLE, 1: UN_CONNECTED, 2: CONNECTED_MOBILE, 3: CONNECTED_WIFI
 */
private static int getNetworkType(Context context) {
    ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectMgr.getActiveNetworkInfo();
    if (networkInfo == null) {
        /** No network */
        return NetState.NO_AVAILABLE;
    }
    if (!networkInfo.isConnected()) {
        /** The network is disconnected or closed */
        return NetState.UN_CONNECTED;
    }
    if (networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
        /** Ethernet network */
        return NetState.CONNECTED_MOBILE;
    } else if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
        /** WiFi network, when activated, all data traffic will use this connection by default */
        return NetState.CONNECTED_WIFI;
    } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
        /** Mobile data connection, cannot coexist with connection, if wifi is turned on, it is automatically closed */
        switch (networkInfo.getSubtype()) {
            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:
                /** 2G network */
                NetSubState.NET_SUB_STATUS = NetSubState.NETWORK_TYPE_2G;
                return NetState.CONNECTED_MOBILE;

            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:
                /** 3G network */
                NetSubState.NET_SUB_STATUS = NetSubState.NETWORK_TYPE_3G;
                return NetState.CONNECTED_MOBILE;

            case TelephonyManager.NETWORK_TYPE_LTE:
                /** 4G network */
                NetSubState.NET_SUB_STATUS = NetSubState.NETWORK_TYPE_4G;
                return NetState.CONNECTED_MOBILE;

            default:
                /** Default 4G network */
                NetSubState.NET_SUB_STATUS = NetSubState.NETWORK_TYPE_4G;
                return NetState.CONNECTED_MOBILE;
        }
    }
    /** Unknown network */
    return NetState.NO_AVAILABLE;
}
 
Example 19
Source File: NetworkUtils.java    From bleYan with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 是否存在有效的WIFI连接
 */
public static boolean isWifiConnected(Context context) {
	NetworkInfo net = getConnManager(context).getActiveNetworkInfo();
	return net != null && net.getType() == ConnectivityManager.TYPE_WIFI && net.isConnected();
}
 
Example 20
Source File: NetworkUtils.java    From bleYan with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 是否存在有效的移动连接
 * @param context
 * @return boolean
 */
public static boolean isMobileConnected(Context context) {
	NetworkInfo net = getConnManager(context).getActiveNetworkInfo();
	return net != null && net.getType() == ConnectivityManager.TYPE_MOBILE && net.isConnected();
}