我们在做访问的时候都得进行判断是否连网。判断连网也比较简单,就用到了两个类。ConnectivityManager和NetworkInfo。
只要执行下面的代码就可以了。
- ConnectivityManager connMgr = (ConnectivityManager) context
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = connMgr
- .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
- boolean isWifiConn = networkInfo.isConnected();
- networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
- boolean isMobileConn = networkInfo.isConnected();
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
boolean isWifiConn = networkInfo.isConnected();
networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isMobileConn = networkInfo.isConnected();
只要有一个连接就是有网,NetworkInfo就是网络的信息。还有一种更简便的判断是否连网的方法。
- ConnectivityManager connMgr = (ConnectivityManager) context
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
在一个就是监听网络变化,就是设置一个广播。
注册广播,监听ConnectivityManager.CONNECTIVITY_ACTION这个action
- IntentFilter filter = new IntentFilter(
- ConnectivityManager.CONNECTIVITY_ACTION);
- receiver = new NetworkReceiver();
- context.registerReceiver(receiver, filter);
IntentFilter filter = new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION);
receiver = new NetworkReceiver();
context.registerReceiver(receiver, filter);
再实现一个广播类就可以了。这样网络一变化,就会受到广播,然后执行对应操作了。
- public class NetworkReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- Log.i("TAG", "intent============>>>>" + intent.toString());
- }
- }
public class NetworkReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("TAG", "intent============>>>>" + intent.toString());
}
}
下面就是来说一下判断手机网络的类型了,2G,3G,4G
关于网络类型,在得到networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);这个后,确定是手机网络后,通过networkInfo.getSubtype()会得到网络的类型,用networkInfo.getSubtypeName()会得到网络的名字。
我们通过这个networkInfo.getSubtype()得到的类型来判断当前是什么网络。
其实,Android的Api中已经定义了各种网络状态,不是在ConnectivityManager里面定义的,而是在TelephonyManager里面的定义的。通过不同的api版本的修改,目前增加到了14个状态值:
下面是通过自己查资料,和看别人查资料实验得来的。整理如下:
- --------------------Added in API level 1---------------------
- public static final int NETWORK_TYPE_UNKNOWN
- Network type is unknown
- Constant Value: 0 (0x00000000)
- (不知道网络类型)
- public static final int NETWORK_TYPE_GPRS
- Current network is GPRS
- Constant Value: 1 (0x00000001)
- (2.5G)移动和联通
- public static final int NETWORK_TYPE_EDGE
- Current network is EDGE
- Constant Value: 2 (0x00000002)
- (2.75G)2.5G到3G的过渡 移动和联通
- public static final int NETWORK_TYPE_UMTS
- Current network is UMTS
- Constant Value: 3 (0x00000003)
- (3G)联通
- -----------------Added in API level 4---------------------
- public static final int NETWORK_TYPE_CDMA
- Current network is CDMA: Either IS95A or IS95B
- Constant Value: 4 (0x00000004)
- (2G 电信)
- public static final int NETWORK_TYPE_EVDO_0
- Current network is EVDO revision 0
- Constant Value: 5 (0x00000005)
- ( 3G )电信
- public static final int NETWORK_TYPE_EVDO_A
- Current network is EVDO revision A
- Constant Value: 6 (0x00000006)
- (3.5G) 属于3G过渡
- public static final int NETWORK_TYPE_1xRTT
- Current network is 1xRTT
- Constant Value: 7 (0x00000007)
- ( 2G )
- ---------------------Added in API level 5--------------------
- public static final int NETWORK_TYPE_HSDPA
- Current network is HSDPA
- Constant Value: 8 (0x00000008)
- (3.5G )
- public static final int NETWORK_TYPE_HSUPA
- Current network is HSUPA
- Constant Value: 9 (0x00000009)
- ( 3.5G )
- public static final int NETWORK_TYPE_HSPA
- Current network is HSPA
- Constant Value: 10 (0x0000000a)
- ( 3G )联通
- --------------------------Added in API level 8-------------------------
- public static final int NETWORK_TYPE_IDEN
- Current network is iDen
- Constant Value: 11 (0x0000000b)
- (2G )
- --------------------------Added in API level 9-------------------------
- public static final int NETWORK_TYPE_EVDO_B
- Current network is EVDO revision B
- Constant Value: 12 (0x0000000c)
- 3G-3.5G
- --------------------------Added in API level 11------------------------
- public static final int NETWORK_TYPE_LTE
- Current network is LTE
- Constant Value: 13 (0x0000000d)
- (4G)
- public static final int NETWORK_TYPE_EHRPD
- Current network is eHRPD
- Constant Value: 14 (0x0000000e)
- 3G(3G到4G的升级产物)
- --------------------------Added in API level 13---------------------------
- public static final int NETWORK_TYPE_HSPAP
- Current network is HSPA+
- Constant Value: 15 (0x0000000f)
- ( 3G )
--------------------Added in API level 1---------------------
public static final int NETWORK_TYPE_UNKNOWN
Network type is unknown
Constant Value: 0 (0x00000000)
(不知道网络类型)
public static final int NETWORK_TYPE_GPRS
Current network is GPRS
Constant Value: 1 (0x00000001)
(2.5G)移动和联通
public static final int NETWORK_TYPE_EDGE
Current network is EDGE
Constant Value: 2 (0x00000002)
(2.75G)2.5G到3G的过渡 移动和联通
public static final int NETWORK_TYPE_UMTS
Current network is UMTS
Constant Value: 3 (0x00000003)
(3G)联通
-----------------Added in API level 4---------------------
public static final int NETWORK_TYPE_CDMA
Current network is CDMA: Either IS95A or IS95B
Constant Value: 4 (0x00000004)
(2G 电信)
public static final int NETWORK_TYPE_EVDO_0
Current network is EVDO revision 0
Constant Value: 5 (0x00000005)
( 3G )电信
public static final int NETWORK_TYPE_EVDO_A
Current network is EVDO revision A
Constant Value: 6 (0x00000006)
(3.5G) 属于3G过渡
public static final int NETWORK_TYPE_1xRTT
Current network is 1xRTT
Constant Value: 7 (0x00000007)
( 2G )
---------------------Added in API level 5--------------------
public static final int NETWORK_TYPE_HSDPA
Current network is HSDPA
Constant Value: 8 (0x00000008)
(3.5G )
public static final int NETWORK_TYPE_HSUPA
Current network is HSUPA
Constant Value: 9 (0x00000009)
( 3.5G )
public static final int NETWORK_TYPE_HSPA
Current network is HSPA
Constant Value: 10 (0x0000000a)
( 3G )联通
--------------------------Added in API level 8-------------------------
public static final int NETWORK_TYPE_IDEN
Current network is iDen
Constant Value: 11 (0x0000000b)
(2G )
--------------------------Added in API level 9-------------------------
public static final int NETWORK_TYPE_EVDO_B
Current network is EVDO revision B
Constant Value: 12 (0x0000000c)
3G-3.5G
--------------------------Added in API level 11------------------------
public static final int NETWORK_TYPE_LTE
Current network is LTE
Constant Value: 13 (0x0000000d)
(4G)
public static final int NETWORK_TYPE_EHRPD
Current network is eHRPD
Constant Value: 14 (0x0000000e)
3G(3G到4G的升级产物)
--------------------------Added in API level 13---------------------------
public static final int NETWORK_TYPE_HSPAP
Current network is HSPA+
Constant Value: 15 (0x0000000f)
( 3G )
所以我们可以networkInfo.getSubtype()得到值和上面的进行对应,就知道目前是几G网络了。