Android 实时监测(监听)网络连接状态变化,工具类可直接使用

/**
 * Author : 马占柱
 * E-mail : [email protected]
 * Time   : 2022/3/21 11:55
 * Desc   : 网络注册监听
 */
public class NetworkChange {
    public static final String TAG = "NetworkChange";
    private static boolean isRegister = false;
    private static NetStateChangeObserver listener;
    private static BroadcastReceiver Receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
                ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
                if (null != activeNetwork) {
                    if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                        if (listener != null) listener.onWifiConnect();
                    } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                        if (listener != null) listener.onMobileConnect();
                    } else {
                        if (listener != null) listener.onDisconnect();
                    }
                } else {
                    if (listener != null) listener.onDisconnect();
                }
            }
        }
    };

    public static void registerReceiver(Context context, NetStateChangeObserver lis) {
        listener = lis;
        IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        context.getApplicationContext().registerReceiver(Receiver, intentFilter);
        isRegister = true;
    }

    public static void unRegisterReceiver(Context context) {
        try {
            if (isRegister) {
                context.getApplicationContext().unregisterReceiver(Receiver);
            }
        } catch (Exception e) {
            HQLog.e(TAG, "网络注册监听——解除监听报错" + e.toString());
        }
    }

    public interface NetStateChangeObserver {

        void onDisconnect();

        void onMobileConnect();

        void onWifiConnect();
    }
}
package com.faw.hqzl3.newsproxy.util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.faw.hqzl3.hqcoresdk.Log.HQLog;

/**
 * Author : 马占柱
 * E-mail : [email protected]
 * Time   : 2022/3/21 11:55
 * Desc   : 网络注册监听
 */
public class NetworkChange {
    public static final String TAG = "NetworkChange";
    private static boolean isRegister = false;
    private static NetStateChangeObserver listener;
    private static BroadcastReceiver Receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
                ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
                if (null != activeNetwork) {
                    if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                        if (listener != null) listener.onWifiConnect();
                    } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                        if (listener != null) listener.onMobileConnect();
                    } else {
                        if (listener != null) listener.onDisconnect();
                    }
                } else {
                    if (listener != null) listener.onDisconnect();
                }
            }
        }
    };

    public static void registerReceiver(Context context, NetStateChangeObserver lis) {
        listener = lis;
        IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        context.getApplicationContext().registerReceiver(Receiver, intentFilter);
        isRegister = true;
    }

    public static void unRegisterReceiver(Context context) {
        try {
            if (isRegister) {
                context.getApplicationContext().unregisterReceiver(Receiver);
            }
        } catch (Exception e) {
            Log.e(TAG, "网络注册监听——解除监听报错" + e.toString());
        }
    }

    public interface NetStateChangeObserver {

        void onDisconnect();

        void onMobileConnect();

        void onWifiConnect();
    }
}

在需要注册的地方使用:

NetworkChange.registerReceiver(context, this)
NetworkChange.unRegisterReceiver(context)

重写三个方法

@Override
    public void onDisconnect() {//没有网络

    }

    @Override
    public void onMobileConnect() {//移动网络

    }

    @Override
    public void onWifiConnect() {//WIFI网络

    }

你可能感兴趣的:(技术总结,网络通信,网络,android,界面,ui)