动态注册监听网络变化
创建intentFilter 并addAction 代表了监听哪个广播
然后使用registerReceiver()方法 将intentFilter 与 自己创建的监听器 传进去
动态注册的 需要unregisterReciver()
权限需要在Mainfest中声明
public class MainActivity extends AppCompatActivity {
private IntentFilter intentFilter;
private NetworkChangedReceiver networkChangedReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentFilter = new IntentFilter();
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
networkChangedReceiver = new NetworkChangedReceiver();
registerReceiver(networkChangedReceiver,intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(networkChangedRecei