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
|
package cn.hackcoder.beautyreader.broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.util.Log;
import android.widget.Toast;
import cn.hackcoder.beautyreader.activity.base.BaseActivity;
import cn.hackcoder.beautyreader.utils.NetWorkUtils;
public class NetWorkStatusReceiver extends BroadcastReceiver {
public NetWorkStatusReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
Toast.makeText(context, "network changed" , Toast.LENGTH_LONG).show();
BaseActivity.isNetWorkConnected = NetWorkUtils.getAPNType(context)> 0 ;
}
}
}
|
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
package cn.hackcoder.beautyreader.utils;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
/**
* Created by hackcoder on 15-1-25.
*/
public class NetWorkUtils {
/**
* 判断是否有网络连接
* @param context
* @return
*/
public static boolean isNetworkConnected(Context context) {
if (context != null ) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null ) {
return mNetworkInfo.isAvailable();
}
}
return false ;
}
/**
* 判断WIFI网络是否可用
* @param context
* @return
*/
public static boolean isWifiConnected(Context context) {
if (context != null ) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWiFiNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWiFiNetworkInfo != null ) {
return mWiFiNetworkInfo.isAvailable();
}
}
return false ;
}
/**
* 判断MOBILE网络是否可用
* @param context
* @return
*/
public static boolean isMobileConnected(Context context) {
if (context != null ) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobileNetworkInfo != null ) {
return mMobileNetworkInfo.isAvailable();
}
}
return false ;
}
/**
* 获取当前网络连接的类型信息
* @param context
* @return
*/
public static int getConnectedType(Context context) {
if (context != null ) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
return mNetworkInfo.getType();
}
}
return - 1 ;
}
/**
* 获取当前的网络状态 :没有网络0:WIFI网络1:3G网络2:2G网络3
*
* @param context
* @return
*/
public static int getAPNType(Context context) {
int netType = 0 ;
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null ) {
return netType;
}
int nType = networkInfo.getType();
if (nType == ConnectivityManager.TYPE_WIFI) {
netType = 1 ; // wifi
} else if (nType == ConnectivityManager.TYPE_MOBILE) {
int nSubType = networkInfo.getSubtype();
TelephonyManager mTelephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
&& !mTelephony.isNetworkRoaming()) {
netType = 2 ; // 3G
} else {
netType = 3 ; // 2G
}
}
return netType;
}
}
|
注册:
1
2
3
4
5
6
7
8
|
<receiver
android:name= ".broadcast.NetWorkStatusReceiver"
android:enabled= "true"
android:exported= "true" >
<intent-filter>
<action android:name= "android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
|