判断手机的联网类型:
public static void checkNetworkType(Context ctx) {
ConnectivityManager connectivity =
(ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
Log.e(Constants.TAG, "获取网络类型失败");
return;
} else {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if(info == null) {
Log.e(Constants.TAG, "获取网络类型失败");
return;
}
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
TelephonyManager tm = (TelephonyManager)
ctx.getSystemService(Context.TELEPHONY_SERVICE);
int type = tm.getNetworkType();
switch (type) {
case TelephonyManager.NETWORK_TYPE_EDGE:
Log.d(Constants.TAG, "网络类型为EDGE");
break;
case TelephonyManager.NETWORK_TYPE_GPRS:
Log.d(Constants.TAG, "网络类型为GPRS");
break;
case TelephonyManager.NETWORK_TYPE_UMTS:
Log.d(Constants.TAG, "网络类型为UMTS");
break;
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
Log.d(Constants.TAG, "网络类型未知");
break;
}
} else if (info.getType() == ConnectivityManager.TYPE_WIFI){
Log.d(Constants.TAG, "网络类型为WIFI");
}
}
}