网络状态监听

监听网络变化在开发中是经常用到的,例如我们断网有一些友好的提示,或者根据不同的网络更改一些加载策略,例如wifi看视频,非wifi则会有一个提示,等等情况。

在5.0以前,我们都是广播BroadcastReceiver,注册跟网络变化相关的广播,然后判断是连接还是断开,这种做法非常方便,但是随着安卓的版本迭代,在权限上越来越谨慎,广播的方式就显得不太优雅。

所以在安卓5.0以上终于对网络的监听进行了优化,那就是通过Callback回调的方式,这种开发模式是不是很常用?例如监听下载进度,我只需要三个回调:下载成功,下载失败,下载的进度变化,这种回调方式针对性强,耦合性低,非常方便。

为此我借鉴了大牛们的一些做法以及代码,写了一点东西,希望5.0之前和之后都能兼容到。

主要涉及到三个核心类:
代码如下:
NetworkCallbackUtil.java

import android.content.Context;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build;

/**
 * 监听网络连接状态
 * 主要作用类还是 NetworkCallbackImpl
 */
public class NetworkCallbackUtil {

    /**
     * 在application中调用
     */
    public static void register(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            NetworkCallbackImpl.getInstance().register(context);
        } else {
            NetworkChangBroadcast.getInstance().register(context);
        }
    }

    public static void addNetworkCallback(Object tag, ConnectivityChangeCallback call) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            NetworkCallbackImpl.getInstance().addNetworkCallback(tag, call);
        } else {
            NetworkChangBroadcast.getInstance().addNetworkCallback(tag, call);
        }
    }

    public static void removeNetworkCallback(Object tag) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            NetworkCallbackImpl.getInstance().removeNetworkCallback(tag);
        } else {
            NetworkChangBroadcast.getInstance().removeNetworkCallback(tag);
        }
    }


    /**
     * 网络状态监听接口
     * 可模仿ConnectivityManager.NetworkCallback的接口方法(在NetworkCallbackImpl中),目前只用到两个
     * Network network:兼容NetworkCallbackImpl
     * NetworkInfo networkInfo:兼容NetworkChangBroadcast
     */
    public interface ConnectivityChangeCallback {
        void onAvailable(Network network, NetworkInfo networkInfo);

        void onLost(Network network, NetworkInfo networkInfo);
    }
}

NetworkCallbackImpl.java:


import android.content.Context;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.os.Build;
import android.support.annotation.RequiresApi;

import java.util.HashMap;
import java.util.Map;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class NetworkCallbackImpl {
    private ConnectivityManager.NetworkCallback networkCallback;
    private Map<Object, NetworkCallbackUtil.ConnectivityChangeCallback> callbackList = new HashMap<>();

    private NetworkCallbackImpl() {
        networkCallback = new ConnectivityManager.NetworkCallback() {
            /**
             * 网络可用的回调
             */
            @Override
            public void onAvailable(Network network) {
                super.onAvailable(network);
//                Toast.makeText(context, "onAvailable:", Toast.LENGTH_SHORT).show();
                for (NetworkCallbackUtil.ConnectivityChangeCallback callback : callbackList.values()) {
                    callback.onAvailable(network, null);
                }
            }

            /**
             * 在网络失去连接的时候回调,但是如果是一个生硬的断开,他可能不回调
             */
            @Override
            public void onLosing(Network network, int maxMsToLive) {
                super.onLosing(network, maxMsToLive);
//        Toast.makeText(context, "onLosing", Toast.LENGTH_SHORT).show();
            }

            /**
             * 网络丢失的回调
             */
            @Override
            public void onLost(Network network) {
                super.onLost(network);
//        Toast.makeText(context, "onLost", Toast.LENGTH_SHORT).show();
                for (NetworkCallbackUtil.ConnectivityChangeCallback callback : callbackList.values()) {
                    callback.onLost(network, null);
                }
            }

            /**
             * 按照官方的字面意思是,当我们的网络的某个能力发生了变化回调,那么也就是说可能会回调多次
             * <p>
             * 之后在仔细的研究
             */
            @Override
            public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
                super.onCapabilitiesChanged(network, networkCapabilities);
//        Toast.makeText(context, "onCapabilitiesChanged", Toast.LENGTH_SHORT).show();
            }

            /**
             * 当建立网络连接时,回调连接的属性
             */
            @Override
            public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {
                super.onLinkPropertiesChanged(network, linkProperties);
//        Toast.makeText(context, "onLinkPropertiesChanged", Toast.LENGTH_SHORT).show();
            }

            /**
             * 按照官方注释的解释,是指如果在超时时间内都没有找到可用的网络时进行回调
             */
            @Override
            public void onUnavailable() {
                super.onUnavailable();
//        Toast.makeText(context, "onUnavailable", Toast.LENGTH_SHORT).show();
            }
        };
    }

    private static class SingletonHolder {
        /**
         * 静态初始化器,由JVM来保证线程安全
         */
        public static final NetworkCallbackImpl INSTANCE = new NetworkCallbackImpl();
    }

    public static NetworkCallbackImpl getInstance() {
        return SingletonHolder.INSTANCE;
    }

    /**
     * 在application中调用
     */
    public void register(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            NetworkRequest.Builder builder = new NetworkRequest.Builder();
            NetworkRequest request = builder.build();
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            // 请注意这里会有一个版本适配bug,所以请在这里添加非空判断
            if (mConnectivityManager != null) {
                mConnectivityManager.registerNetworkCallback(request, networkCallback);
            }
        }
    }

    public void addNetworkCallback(Object tag, NetworkCallbackUtil.ConnectivityChangeCallback call) {
        callbackList.put(tag, call);
    }

    public void removeNetworkCallback(Object tag) {
        callbackList.remove(tag);
    }

}

BroadcastReceiver类:


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 java.util.HashMap;
import java.util.Map;

public class NetworkChangBroadcast {
    private BroadcastReceiver mNetworkChangBroadcast;
    private Map<Object, NetworkCallbackUtil.ConnectivityChangeCallback> callbackList = new HashMap<>();

    private static class SingletonHolder {
        static final NetworkChangBroadcast INSTANCE = new NetworkChangBroadcast();
    }

    public static NetworkChangBroadcast getInstance() {
        return NetworkChangBroadcast.SingletonHolder.INSTANCE;
    }

    private NetworkChangBroadcast() {
        mNetworkChangBroadcast = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                if (networkInfo != null && networkInfo.isAvailable()) {
//            Toast.makeText(context, "网络已连接", Toast.LENGTH_SHORT).show();
                    for (NetworkCallbackUtil.ConnectivityChangeCallback callback : callbackList.values()) {
                        callback.onAvailable(null, networkInfo);
                    }
                } else {
//            Toast.makeText(context, "网络断开", Toast.LENGTH_SHORT).show();
                    for (NetworkCallbackUtil.ConnectivityChangeCallback callback : callbackList.values()) {
                        callback.onLost(null, networkInfo);
                    }
                }
            }
        };
    }

    public void register(Context context) {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        context.registerReceiver(mNetworkChangBroadcast, intentFilter);
    }

    public void unregister(Context context) {
        context.unregisterReceiver(mNetworkChangBroadcast);
    }

    public void addNetworkCallback(Object tag, NetworkCallbackUtil.ConnectivityChangeCallback call) {
        callbackList.put(tag, call);
    }

    public void removeNetworkCallback(Object tag) {
        callbackList.remove(tag);
    }


}

使用示例:
1,所需权限:

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

2,,在applicationg中进行注册

 @Override
    public void onCreate() {
        super.onCreate();
        NetworkCallbackUtil.register(this);
    }

3,在基础Activity类中,添加和移除监听接口:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NetworkCallbackUtil.addNetworkCallback(this.getClass().getName(), new NetworkCallbackUtil.ConnectivityChangeCallback() {
            @Override
            public void onAvailable(Network network, NetworkInfo networkInfo) {
             //网络可用的回调
            }

            @Override
            public void onLost(Network network, NetworkInfo networkInfo) {
              //网络丢失的回调
            }
        });
    }

@Override
    protected void onDestroy() {
        super.onDestroy();
        NetworkCallbackUtil.removeNetworkCallback(this.getClass().getName());
    }

后言感谢:

android 5.0 以上监听网络变化
Android 7.0 监听网络变化的示例代码
Android监听网络变化练习(1)
Android实时监听网络的变化

推荐阅读更多精彩内容