Android 监测网络切换

//Activity注册
NetMonitor netMonitor = new NetMonitor();
netMonitor.start(this.getApplicationContext());
netMonitor.addListener(this);

//网络监听的回调

@Override public void onNetworkChange() { Log.e("TAG", "回调"); }

//注册动态广播实时监听

public class NetMonitor {
    private List<OnNetworkChangeListener> onNetworkChangeListeners;
    private NetBroadcastReceiver netBroadcastReceiver;
    private Context context;

    private ExecutorService executor = Executors.newCachedThreadPool();
    private Runnable netChangeRunnable = new Runnable() {
        @Override
        public void run() {
            notifyNetworkChange();
        }
    };

    public NetMonitor() {
        onNetworkChangeListeners = new ArrayList<>();
    }

    /**
     * 开启网络监听
     */
    public void start(Context context) {
        Context applicationContext = context.getApplicationContext();
        this.context = applicationContext;

        netBroadcastReceiver = new NetBroadcastReceiver();
        IntentFilter filter = new IntentFilter();

        //设置action为CONNECTIVITY_ACTION
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        //动态注册广播
        applicationContext.registerReceiver(netBroadcastReceiver, filter);
    }

    /**
     * 停止网络监听
     */
    public void stop() {
        if (context == null) {
            return;
        }

        if (netBroadcastReceiver != null) {
            context.unregisterReceiver(netBroadcastReceiver);
        }

        context = null;
    }

    /**
     * 注册监听
     */
    public void addListener(OnNetworkChangeListener listener) {
        if (listener != null) {
            onNetworkChangeListeners.add(listener);
        }
    }

    /**
     * 注销监听
     */
    public void removeListener(OnNetworkChangeListener listener) {
        if (listener != null) {
            onNetworkChangeListeners.remove(listener);
        }
    }

    /**
     * 通知网络状态更新
     */
    private void notifyNetworkChange() {
        for (OnNetworkChangeListener listener : onNetworkChangeListeners) {
            if (listener != null) {
                listener.onNetworkChange();
            }
        }
    }

    private class NetBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            //如果action是网络变化,则执行以下处理逻辑
            if (action != null && action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                //网络状态可以根据自己项目工具类获取
                int type = NetworkUtils.getNetWorkType(TextApp.getInstance());
                if (type == NetworkUtils.NETWORK_NO) {
                    Log.e("TAG", "断开");
                    executor.execute(netChangeRunnable);
                } else if (type != NetworkUtils.NETWORK_WIFI) {
                    executor.execute(netChangeRunnable);
                    Log.e("TAG", "连上移动网络了");
                }else {
                    Log.e("TAG", "连上(wifi)");
                }
            }

        }
    }
}

//接口

 

public interface OnNetworkChangeListener {
    void onNetworkChange();
}

 

 

上一篇:零基础教程!一文教你使用Rancher 2.3和Terraform运行Windows容器


下一篇:springboot 监听器listener的使用