下面列出了android.net.NetworkInfo#getType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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();
}
@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;
}
}
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;
}
}
/**
* 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);
}
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;
}
public Type getType() {
NetworkInfo network = mConnectivityManager.getActiveNetworkInfo();
if (network == null) return NONE;
if (network.getType() == ConnectivityManager.TYPE_WIFI) {
return WIFI;
} else return MOBILE;
}
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;
}
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()));
}
}
}
}
@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);
}
}
}
}
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;
}
/**
* 检测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;
}
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;
}
/**
* 判断是否有可用状态的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;
}
public Boolean isWifiConnected(){
NetworkInfo networkInfo = getNetworkInfo();
return networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
}
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;
}
@RequiresPermission(ACCESS_NETWORK_STATE)
public static boolean isMobileData() {
NetworkInfo info = getActiveNetworkInfo();
return null != info && info.isAvailable() && info.getType() == ConnectivityManager.TYPE_MOBILE;
}
/**
* 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;
}
/**
* 是否存在有效的WIFI连接
*/
public static boolean isWifiConnected(Context context) {
NetworkInfo net = getConnManager(context).getActiveNetworkInfo();
return net != null && net.getType() == ConnectivityManager.TYPE_WIFI && net.isConnected();
}
/**
* 是否存在有效的移动连接
* @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();
}