王学岗网络监听

网上找了好久,终于找到啦,自己改了下

 private var networkCallback = object : ConnectivityManager.NetworkCallback() {
        override fun onCapabilitiesChanged(
            network: Network,
            networkCapabilities: NetworkCapabilities
        ) {
            super.onCapabilitiesChanged(network, networkCapabilities)

            //表明此网络连接验证成功
            if(networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
                if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                    Log.d(TAG, "===当前在使用Mobile流量上网===");
                } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                    Log.d(TAG, "====当前在使用WiFi上网===");
                } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH)) {
                    Log.d(TAG, "=====当前使用蓝牙上网=====");
                } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
                    Log.d(TAG, "=====当前使用以太网上网=====");
                } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
                    Log.d(TAG, "===当前使用VPN上网====");
                } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI_AWARE)) {
                    Log.d(TAG, "===表示此网络使用Wi-Fi感知传输====");
                } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_LOWPAN)) {
                    Log.d(TAG, "=====表示此网络使用LoWPAN传输=====");
                } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_USB)) {
                    Log.d(TAG, "=====表示此网络使用USB传输=====");
                }
            }

        }

        override fun onAvailable(network: Network) {
            super.onAvailable(network)
            Log.e(TAG, "==网络连接成功,通知可以使用的时候调用====onAvailable===")
        }

        override fun onUnavailable() {
            super.onUnavailable()
            Log.e(TAG, "==当网络连接超时或网络请求达不到可用要求时调用====onUnavailable===")
        }

        override fun onBlockedStatusChanged(network: Network, blocked: Boolean) {
            super.onBlockedStatusChanged(network, blocked)
            Log.e(TAG, "==当访问指定的网络被阻止或解除阻塞时调用===onBlockedStatusChanged==");
        }

        override fun onLosing(network: Network, maxMsToLive: Int) {
            super.onLosing(network, maxMsToLive)
            Log.e(TAG, "==当网络正在断开连接时调用===onLosing===")
        }

        override fun onLost(network: Network) {
            super.onLost(network)
            Log.e(TAG, "==当网络已断开连接时调用===onLost===")
        }

        override fun onLinkPropertiesChanged(network: Network, linkProperties: LinkProperties) {
            super.onLinkPropertiesChanged(network, linkProperties)
            Log.e(TAG, "==当网络连接的属性被修改时调用===onLinkPropertiesChanged===")
        }
    }

注册与解除注册

    lateinit var connectivityManager: ConnectivityManager
    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        connectivityManager = this.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
        connectivityManager.registerDefaultNetworkCallback(networkCallback)
    }

    override fun onDestroy() {
        super.onDestroy()
        connectivityManager.unregisterNetworkCallback(networkCallback)
    }

另有工具类(也是复制粘贴大佬们的)

@SuppressLint("NewApi")
public class NetworkUtils {
 
    private static final String TAG = "ConnectManager";
 
    /**
     * Indicates this network uses a Cellular transport.
     */
    public static final int TRANSPORT_CELLULAR = 0;
 
    /**
     * Indicates this network uses a Wi-Fi transport.
     */
    public static final int TRANSPORT_WIFI = 1;
 
    /**
     * Indicates this network uses a Bluetooth transport.
     */
    public static final int TRANSPORT_BLUETOOTH = 2;
 
    /**
     * Indicates this network uses an Ethernet transport.
     */
    public static final int TRANSPORT_ETHERNET = 3;
 
    /**
     * Indicates this network uses a VPN transport.
     */
    public static final int TRANSPORT_VPN = 4;
 
    /**
     * Indicates this network uses a Wi-Fi Aware transport.
     */
    public static final int TRANSPORT_WIFI_AWARE = 5;
 
    /**
     * Indicates this network uses a LoWPAN transport.
     */
    public static final int TRANSPORT_LOWPAN = 6;
 
    /**
     * Indicates this network uses a Test-only virtual interface as a transport.
     * @hide
     */
    public static final int TRANSPORT_TEST = 7;
 
    /**
     * Indicates this network uses a USB transport.
     */
    public static final int TRANSPORT_USB = 8;
 
 
 
    /**
     * >= Android 10(Q版本)推荐
     *
     * 当前使用MOBILE流量上网
     */
    public static boolean isMobileNetwork(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
 
        Network network = cm.getActiveNetwork();
        if (null == network) {
            return false;
        }
 
        NetworkCapabilities capabilities = cm.getNetworkCapabilities(network);
        if (null == capabilities) {
            return false;
        }
        return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
                && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
    }
 
 
    /**
     * >= Android 10(Q版本)推荐
     *
     * 当前使用WIFI上网
     */
 
    public static boolean isWifiNetwork(Context context) {
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Network network = cm.getActiveNetwork();
        if (null == network) {
            return false;
        }
        NetworkCapabilities capabilities = cm.getNetworkCapabilities(network);
        if (null == capabilities) {
            return false;
        }
        return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
                && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
    }
 
 
    /**
     *  >= Android 10(Q版本)推荐
     *
     * 当前使用以太网上网
     */
    public static boolean isEthernetNetwork(Context context) {
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Network network = cm.getActiveNetwork();
        if (null == network) {
            return false;
        }
        NetworkCapabilities capabilities = cm.getNetworkCapabilities(network);
        if (null == capabilities) {
            return false;
        }
        return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)
                && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
    }
 
 
 
    /**
     * >= Android 10(Q版本)推荐
     *
     * NetworkCapabilities.NET_CAPABILITY_INTERNET,表示此网络应该(maybe)能够访问internet
     *
     *  判断当前网络可以正常上网
     *  表示此连接此网络并且能成功上网。  例如,对于具有NET_CAPABILITY_INTERNET的网络,这意味着已成功检测到INTERNET连接。
     */
    public static boolean isConnectedAvailableNetwork(Context context) {
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Network network = cm.getActiveNetwork();
        if (null == network) {
            return false;
        }
        NetworkCapabilities capabilities = cm.getNetworkCapabilities(network);
        if (null == capabilities) {
            return false;
        }
        return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
    }
 
 
    /**
     * >= Android 10(Q版本)推荐
     *
     * 获取成功上网的网络类型
     * value = {
     *    TRANSPORT_CELLULAR,   0 表示此网络使用蜂窝传输。
     *    TRANSPORT_WIFI,       1 表示此网络使用Wi-Fi传输。
     *    TRANSPORT_BLUETOOTH,  2 表示此网络使用蓝牙传输。
     *    TRANSPORT_ETHERNET,   3 表示此网络使用以太网传输。
     *    TRANSPORT_VPN,        4 表示此网络使用VPN传输。
     *    TRANSPORT_WIFI_AWARE, 5 表示此网络使用Wi-Fi感知传输。
     *    TRANSPORT_LOWPAN,     6 表示此网络使用LoWPAN传输。
     *    TRANSPORT_TEST,       7 指示此网络使用仅限测试的虚拟接口作为传输。
     *    TRANSPORT_USB,        8 表示此网络使用USB传输
     * }
     */
    public static int getConnectedNetworkType(Context context) {
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Network network = cm.getActiveNetwork();
        if (null == network) {
            return -1;
        }
        NetworkCapabilities capabilities = cm.getNetworkCapabilities(network);
        if (null == capabilities) {
            return -1;
        }
        if (capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
            if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                return NetworkCapabilities.TRANSPORT_CELLULAR;
            } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                return NetworkCapabilities.TRANSPORT_WIFI;
            } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH)) {
                return NetworkCapabilities.TRANSPORT_BLUETOOTH;
            } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
                return NetworkCapabilities.TRANSPORT_ETHERNET;
            } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
                return NetworkCapabilities.TRANSPORT_VPN;
            } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI_AWARE)) {
                return NetworkCapabilities.TRANSPORT_WIFI_AWARE;
            } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_LOWPAN)) {
                return NetworkCapabilities.TRANSPORT_LOWPAN;
            } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_USB)) {
                return NetworkCapabilities.TRANSPORT_USB;
            }
        }
        return -1;
    }
}
相关推荐
小伍_Five42 分钟前
透视网络世界:计算机网络习题的深度解析与总结【前3章】
服务器·网络·计算机网络
zhangphil2 小时前
Android绘图Path基于LinearGradient线性动画渐变,Kotlin(2)
android·kotlin
watl02 小时前
【Android】unzip aar删除冲突classes再zip
android·linux·运维
键盘上的蚂蚁-2 小时前
PHP爬虫类的并发与多线程处理技巧
android
网络安全(king)3 小时前
网络安全攻防学习平台 - 基础关
网络·学习·web安全
李白你好3 小时前
家用无线路由器的 2.4GHz 和 5GHz
运维·网络
嵌入(师)3 小时前
嵌入式驱动开发详解21(网络驱动开发)
网络·驱动开发
喜欢猪猪3 小时前
Java技术专家视角解读:SQL优化与批处理在大数据处理中的应用及原理
android·python·adb
柒烨带你飞4 小时前
路由器的原理
网络·智能路由器·php
xserver24 小时前
ensp 基于EASY IP的公司出口链路配置
网络·tcp/ip·智能路由器