首次提交
This commit is contained in:
100
nebula-sdk/src/main/java/com/android/util/NetworkUtil.java
Normal file
100
nebula-sdk/src/main/java/com/android/util/NetworkUtil.java
Normal file
@@ -0,0 +1,100 @@
|
||||
package com.android.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.telephony.TelephonyManager;
|
||||
|
||||
public class NetworkUtil {
|
||||
private NetworkUtil() {
|
||||
throw new UnsupportedOperationException("u can't instantiate me...");
|
||||
}
|
||||
|
||||
private static NetworkInfo getActiveNetworkInfo(Context context) {
|
||||
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
return cm.getActiveNetworkInfo();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前网络类型
|
||||
* 需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}
|
||||
*/
|
||||
public static NetworkType getNetworkType(Context context) {
|
||||
NetworkType netType = NetworkType.NETWORK_NO;
|
||||
NetworkInfo info = getActiveNetworkInfo(context);
|
||||
if (info != null && info.isAvailable()) {
|
||||
|
||||
if (info.getType() == ConnectivityManager.TYPE_WIFI) {
|
||||
netType = NetworkType.NETWORK_WIFI;
|
||||
} else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
|
||||
switch (info.getSubtype()) {
|
||||
case TelephonyManager.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 = NetworkType.NETWORK_3G;
|
||||
break;
|
||||
|
||||
case TelephonyManager.NETWORK_TYPE_LTE:
|
||||
case TelephonyManager.NETWORK_TYPE_IWLAN:
|
||||
netType = NetworkType.NETWORK_4G;
|
||||
break;
|
||||
|
||||
case TelephonyManager.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 = NetworkType.NETWORK_2G;
|
||||
break;
|
||||
default:
|
||||
String subtypeName = info.getSubtypeName();
|
||||
if (subtypeName.equalsIgnoreCase("TD-SCDMA")
|
||||
|| subtypeName.equalsIgnoreCase("WCDMA")
|
||||
|| subtypeName.equalsIgnoreCase("CDMA2000")) {
|
||||
netType = NetworkType.NETWORK_3G;
|
||||
} else {
|
||||
netType = NetworkType.NETWORK_UNKNOWN;
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else if(info.getType()==ConnectivityManager.TYPE_ETHERNET){
|
||||
netType = NetworkType.NETWORK_ETHERNET;
|
||||
} else {
|
||||
netType = NetworkType.NETWORK_UNKNOWN;
|
||||
}
|
||||
}
|
||||
return netType;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static boolean isActive(Context context) {
|
||||
NetworkInfo info = getActiveNetworkInfo(context);
|
||||
if (info != null && info.isAvailable() && info.isConnected()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user