Android 8.0 BroadcastReceiver静态注册大部分取消,动态注册,有序广播

Android 8.0 移除大部分静态注册广播:https://developer.android.com/reference/android/content/BroadcastReceiver
解决方案:动态注册广播

在Activity中对广播接收进行动态注册实现全局网络监听

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
public class NetWorkStateReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
NLog.i(Constants.TAG, "网络状态发生变化");
//检测API是不是小于21,因为到了API21之后getNetworkInfo(int networkType)方法被弃用
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {

//获得ConnectivityManager对象
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

//获取ConnectivityManager对象对应的NetworkInfo对象
//获取WIFI连接的信息
NetworkInfo wifiNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
//获取移动数据连接的信息
NetworkInfo dataNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) {
//Toast.makeText(context, "WIFI已连接,移动数据已连接", Toast.LENGTH_SHORT).show();
} else if (wifiNetworkInfo.isConnected() && !dataNetworkInfo.isConnected()) {
//Toast.makeText(context, "WIFI已连接,移动数据已断开", Toast.LENGTH_SHORT).show();
} else if (!wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) {
//Toast.makeText(context, "WIFI已断开,移动数据已连接", Toast.LENGTH_SHORT).show();
} else {
//Toast.makeText(context, "WIFI已断开,移动数据已断开", Toast.LENGTH_SHORT).show();
}
} else {
NLog.i(Constants.TAG, "API level 大于21");
//获得ConnectivityManager对象
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

//获取所有网络连接的信息
Network[] networks = connMgr.getAllNetworks();
//用于存放网络连接信息
StringBuilder sb = new StringBuilder();
//通过循环将网络信息逐个取出来
for (int i = 0; i < networks.length; i++) {
//获取ConnectivityManager对象对应的NetworkInfo对象
NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]);
sb.append(networkInfo.getTypeName() + " connect is " + networkInfo.isConnected());
}
Toast.makeText(context, sb.toString(), Toast.LENGTH_SHORT).show();
}
}

}

动态注册广播,最好是在BaseActivity中使用

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
public abstract class BaseActivity  extends AppCompatActivity {
private ImmersionBar mImmersionBar;
//网络状态监听库
NetWorkStateReceiver netWorkStateReceiver;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
//iconfont的统一使用
LayoutInflaterCompat.setFactory(getLayoutInflater(), new IconfontLayoutFactory(this, getDelegate()));
super.onCreate(savedInstanceState);
setContentView(getViewId());
mImmersionBar = ImmersionBar.with(this).statusBarColor(R.color.colorPrimary);
mImmersionBar.init(); //所有子类都将继承这些相同的属性
ButterKnife.bind(this);
initViews();

}

public abstract int getViewId();

@Override
protected void onDestroy() {
super.onDestroy();
if (mImmersionBar != null)
mImmersionBar.destroy(); //必须调用该方法,防止内存泄漏,不调用该方法,如果界面bar发生改变,在不关闭app的情况下,退出此界面再进入将记忆最后一次bar改变的状态
}

protected abstract void initViews();

@Override
protected void onResume() {
if (netWorkStateReceiver == null) {
netWorkStateReceiver = new NetWorkStateReceiver();
}
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(netWorkStateReceiver, filter);
LogUtils.e("注册");
super.onResume();
}

//onPause()方法注销
@Override
protected void onPause() {
unregisterReceiver(netWorkStateReceiver);
LogUtils.e("注销");
super.onPause();
}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :