Android 判断网络是否可用、网络类型WIFI/2G/3G/4G及获取IP

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/csdnadcode/article/details/39012281

1. 在AndroidManifest.xml中添加访问当前网络状态权限

    <!-- 访问当前网络状态权限 -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
    </uses-permission>

2. 添加代码

检查当前网络是否可用

public class LoginActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		if (isNetworkAvailable(LoginActivity.this)) {
			Toast.makeText(getApplicationContext(), "当前有可用网络!",
					Toast.LENGTH_LONG).show();
		} else {
			Toast.makeText(getApplicationContext(), "当前没有可用网络!",
					Toast.LENGTH_LONG).show();
		}
	}

	/**
	 * 检查当前网络是否可用
	 * 
	 * @param context
	 * @return
	 */

	public boolean isNetworkAvailable(Activity activity) {
		Context context = activity.getApplicationContext();
		// 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		if (connectivityManager == null) {
			return false;
		} else {
			// 获取NetworkInfo对象
			NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

			if (networkInfo != null && networkInfo.length > 0) {
				for (int i = 0; i < networkInfo.length; i++) {
					// 判断当前网络状态是否为连接状态
					if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}
}


3. 判断手机网络类型

 ConnectivityManager connectMgr = (ConnectivityManager) this
        .getSystemService(Context.CONNECTIVITY_SERVICE);
 
 NetworkInfo info = connectMgr.getActiveNetworkInfo();
 
一、判断网络是否是wifi,在判断之前一定要进行的非空判断,如果没有任何网络
连接info ==null
info.getType() == ConnectivityManager.TYPE_WIFI
 
二、判断是否是手机网络
info !=null && info.getType() ==  ConnectivityManager.TYPE_MOBILE
 
手机网络进行详细区分:
 
info.getSubtype() 这里使用 getSubtype(),不是 getType(),getType()返回的
是0,或者1,是区分是手机网络还是wifi
 
info.getSubtype()取值列表如下:

case NETWORK_TYPE_GPRS:
case NETWORK_TYPE_EDGE:
case NETWORK_TYPE_CDMA:
case NETWORK_TYPE_1xRTT:
case NETWORK_TYPE_IDEN:
return NETWORK_CLASS_2_G;


case NETWORK_TYPE_UMTS:
case NETWORK_TYPE_EVDO_0:
case NETWORK_TYPE_EVDO_A:
case NETWORK_TYPE_HSDPA:
case NETWORK_TYPE_HSUPA:
case NETWORK_TYPE_HSPA:
case NETWORK_TYPE_EVDO_B:
case NETWORK_TYPE_EHRPD:
case NETWORK_TYPE_HSPAP:
return NETWORK_CLASS_3_G;


case NETWORK_TYPE_LTE:
return NETWORK_CLASS_4_G;



GPRS        2G(2.5) General Packet Radia Service 114kbps
EDGE        2G(2.75G) Enhanced Data Rate for GSM Evolution 384kbps
IDEN          2G Integrated Dispatch Enhanced Networks 集成数字增强型网络
CDMA        2G 电信 Code Division Multiple Access 码分多址
1xRTT        2G CDMA2000 1xRTT (RTT - 无线电传输技术) 144kbps 2G的过渡,

EVDO_B    3G EV-DO Rev.B 14.7Mbps 下行 3.5G
EHRPD      3G CDMA2000向LTE 4G的中间产物 Evolved High Rate Packet Data HRPD的升级
HSPA         3G (分HSDPA,HSUPA) High Speed Packet Access 
HSPAP       3G HSPAP 
UMTS        3G WCDMA 联通3G Universal Mobile Telecommunication System 完整的3G移动通信技术标准
EVDO_0    3G (EVDO 全程 CDMA2000 1xEV-DO) Evolution - Data Only (Data Optimized) 153.6kps - 2.4mbps 属于3G
EVDO_A    3G 1.8mbps - 3.1mbps 属于3G过渡,3.5G

HSDPA      3.5G 高速下行分组接入 3.5G WCDMA High Speed Downlink Packet Access 14.4mbps 
HSUPA      3.5G High Speed Uplink Packet Access 高速上行链路分组接入 1.4 - 5.8 mbps

LTE            4G Long Term Evolution FDD-LTE 和 TDD-LTE , 3G过渡,升级版 LTE Advanced 才是4G 



4. Android获取手机IP

private String getPhoneIp() {
        try {  
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {  
                NetworkInterface intf = en.nextElement();  
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {  
                    InetAddress inetAddress = enumIpAddr.nextElement();  
                    if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {  
                    //if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet6Address) {  
                        return inetAddress.getHostAddress().toString();  
                    }  
                }  
            }  
        } catch (Exception e) {  
        }  
        return ""; 
    }





                                    
展开阅读全文

没有更多推荐了,返回首页