package com.bwei.ydhl.utils;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;
/**
* Created by muhanxi on 17/4/10.
*/
public class NetUtil {
/**
* 判断当前是否有网络可用
* @param mActivity
* @return
*/
public static boolean isNetworkAvailable(Context mActivity) {
ConnectivityManager connectivity = (ConnectivityManager) mActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
/**
* 返回当前网络类型
* @param context
* @return
*/
public static String GetNetype(Context context)
{
String strNetworkType = "unknown";
//获取 连接管理器
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
{
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
{
strNetworkType = "WIFI";
}
//当前链接的是手机网络
else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
{
String _strSubTypeName = networkInfo.getSubtypeName();
// TD-SCDMA networkType is 17
int networkType = networkInfo.getSubtype();
switch (networkType) {
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: //api<8 : replace by 11
strNetworkType = "2G";
break;
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: //api<9 : replace by 14
case TelephonyManager.NETWORK_TYPE_EHRPD: //api<11 : replace by 12
case TelephonyManager.NETWORK_TYPE_HSPAP: //api<13 : replace by 15
strNetworkType = "3G";
break;
case TelephonyManager.NETWORK_TYPE_LTE: //api<11 : replace by 13
strNetworkType = "4G";
break;
default:
// http://baike.baidu.com/item/TD-SCDMA 中国移动 联通 电信 三种3G制式
if (_strSubTypeName.equalsIgnoreCase("TD-SCDMA") || _strSubTypeName.equalsIgnoreCase("WCDMA") || _strSubTypeName.equalsIgnoreCase("CDMA2000"))
{
strNetworkType = "3G";
}
else
{
strNetworkType = _strSubTypeName;
}
break;
}
}
}
return strNetworkType;
}
/**
* 跳转到网络设置界面
* @param context
*/
public static void toSystemSetting(Context context){
Intent intent=new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
context.startActivity(intent);
}
}
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;
/**
* Created by muhanxi on 17/4/10.
*/
public class NetUtil {
/**
* 判断当前是否有网络可用
* @param mActivity
* @return
*/
public static boolean isNetworkAvailable(Context mActivity) {
ConnectivityManager connectivity = (ConnectivityManager) mActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
/**
* 返回当前网络类型
* @param context
* @return
*/
public static String GetNetype(Context context)
{
String strNetworkType = "unknown";
//获取 连接管理器
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
{
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
{
strNetworkType = "WIFI";
}
//当前链接的是手机网络
else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
{
String _strSubTypeName = networkInfo.getSubtypeName();
// TD-SCDMA networkType is 17
int networkType = networkInfo.getSubtype();
switch (networkType) {
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: //api<8 : replace by 11
strNetworkType = "2G";
break;
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: //api<9 : replace by 14
case TelephonyManager.NETWORK_TYPE_EHRPD: //api<11 : replace by 12
case TelephonyManager.NETWORK_TYPE_HSPAP: //api<13 : replace by 15
strNetworkType = "3G";
break;
case TelephonyManager.NETWORK_TYPE_LTE: //api<11 : replace by 13
strNetworkType = "4G";
break;
default:
// http://baike.baidu.com/item/TD-SCDMA 中国移动 联通 电信 三种3G制式
if (_strSubTypeName.equalsIgnoreCase("TD-SCDMA") || _strSubTypeName.equalsIgnoreCase("WCDMA") || _strSubTypeName.equalsIgnoreCase("CDMA2000"))
{
strNetworkType = "3G";
}
else
{
strNetworkType = _strSubTypeName;
}
break;
}
}
}
return strNetworkType;
}
/**
* 跳转到网络设置界面
* @param context
*/
public static void toSystemSetting(Context context){
Intent intent=new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
context.startActivity(intent);
}
}