广播,连接网络或者有线,CONNECTIVITY_ACTION过
2020-07-20 本文已影响0人
zhongjh
在讲这个网络广播之前,先区分代码中动态注册
和在Manifest.xml中静态注册
的区别
两种注册广播的不同
代码中动态注册
不是常驻型广播,也就是说广播跟随程序的生命周期。
Manifest.xml中静态注册
是常驻型,也就是说当应用程序关闭后,如果有信息广播来,程序也会被系统调用自动运行。
CONNECTIVITY_ACTION过时
然后现在讲下ConnectivityManager.CONNECTIVITY_ACTION
过时,
里面有段文字是
Apps targeting Android 7.0 (API level 24) and higher do not receive this broadcast if they declare the broadcast receiver in their manifest. Apps will still receive broadcasts if they register their BroadcastReceiver with Context.registerReceiver() and that context is still valid.
直译过来是
针对Android 7.0 (API级别24)或更高的应用程序,如果它们在其manifest中声明广播接收器,则不会接收此广播。如果应用程序将其BroadcastReceiver注册为context . registerreceiver()并且该context仍然有效,那么应用程序仍然会接收广播。
所以记住了7.0版本以上只能代码中动态注册才能接收该广播
代码
public class NetWorkConnectChangedReceiver extends BroadcastReceiver {
private static final String TAG = NetWorkConnectChangedReceiver.class.getSimpleName();
MainActivity mMainActivity;
public NetWorkConnectChangedReceiver(MainActivity mainActivity) {
mMainActivity = mainActivity;
}
@Override
public void onReceive(Context context, Intent intent) {
if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {// 监听wifi的打开与关闭,与wifi的连接无关
int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
Log.i(TAG, "wifiState:" + wifiState);
switch (wifiState) {
case WifiManager.WIFI_STATE_DISABLED:
break;
case WifiManager.WIFI_STATE_DISABLING:
break;
}
}
// 监听wifi的连接状态即是否连上了一个有效无线路由
if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
Parcelable parcelableExtra = intent
.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (null != parcelableExtra) {
// 获取联网状态的NetWorkInfo对象
NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
//获取的State对象则代表着连接成功与否等状态
NetworkInfo.State state = networkInfo.getState();
//判断网络是否已经连接
boolean isConnected = state == NetworkInfo.State.CONNECTED;
Log.i(TAG, "isConnected:" + isConnected);
if (isConnected) {
} else {
}
}
}
// 监听网络连接,包括wifi和移动数据的打开和关闭,以及连接上可用的连接都会接到监听
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
//获取联网状态的NetworkInfo对象
NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (info != null) {
//如果当前的网络连接成功并且网络连接可用
if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) {
Log.i(TAG, getConnectionType(info.getType()) + " 连上");
// 发送UI Event
mMainActivity.showConnectionIcon(getConnectionType(info.getType()));
} else {
Log.i(TAG, getConnectionType(info.getType()) + " 断开");
mMainActivity.hideConnectionIcon(getConnectionType(info.getType()));
}
}
}
}
private int getConnectionType(int type) {
int connType = -1;
if (type == ConnectivityManager.TYPE_MOBILE) {
// 3g网络
connType = NET_WIFI;
} else if (type == ConnectivityManager.TYPE_WIFI) {
connType = NET_WIFI;
} else if (type == ConnectivityManager.TYPE_ETHERNET) {
connType = NET_ETHERNET;
}
return connType;
}
}
常量类
@IntDef({NET_ETHERNET, NET_WIFI, NET_NOCONNECT})
@Retention(RetentionPolicy.SOURCE)
public @interface NetWorkInfo {
int NET_ETHERNET = 0; // 有线连接
int NET_WIFI = 1; // 无线连接
int NET_NOCONNECT = 2; // 没有连接
}