android 判断是否有网络连接
发布网友
发布时间:2022-03-26 01:33
我来回答
共3个回答
热心网友
时间:2022-03-26 03:02
我们在做访问的时候都得进行判断是否连网。判断连网也比较简单,就用到了两个类。ConnectivityManager和NetworkInfo。
只要执行下面的代码就可以了。
1
2
3
4
5
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就是网络的信息。还有一种更简便的判断是否连网的方法。
1
2
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
在一个就是监听网络变化,就是设置一个广播。
注册广播,监听ConnectivityManager.CONNECTIVITY_ACTION这个action
1
2
3
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
receiver = new NetworkReceiver();
context.registerReceiver(receiver, filter);
再实现一个广播类就可以了。这样网络一变化,就会受到广播,然后执行对应操作了。
1
2
3
4
5
6
7
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个状态值:
下面是通过自己查资料,和看别人查资料实验得来的。整理如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
--------------------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 )
热心网友
时间:2022-03-26 04:20
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivity.getActiveNetworkInfo();
if(info != null && info.isAvailable()){
if (netInfo.getType() == ConnectivityManager.TYPE_WIFI) {
//WiFi网络
} else if (netInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
//移动网络
} else {
//网络错误
}
}else{
//网络错误
}
热心网友
时间:2022-03-26 05:55
用无线搜索啊!搜到了就显示了!现在Wi-Fi很多的!