Android之WIFI-网络可用性校验(NetworkMoni

2022-02-03  本文已影响0人  锄禾豆
    public static String getSummary(Context context, String ssid, DetailedState state,
            boolean isEphemeral, String passpointProvider) {
        ······
                        // Case when there is wifi connected without internet connectivity.
        final ConnectivityManager cm = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (state == DetailedState.CONNECTED) {
            IWifiManager wifiManager = IWifiManager.Stub.asInterface(
                    ServiceManager.getService(Context.WIFI_SERVICE));
            NetworkCapabilities nc = null;

            try {
                nc = cm.getNetworkCapabilities(wifiManager.getCurrentNetwork());
            } catch (RemoteException e) {}

            if (nc != null) {
                if (nc.hasCapability(nc.NET_CAPABILITY_CAPTIVE_PORTAL)) {
                    return context.getString(
                        com.android.internal.R.string.network_available_sign_in);//登录到网络
                } else if (!nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
                    return context.getString(R.string.wifi_connected_no_internet);//已连接,但无法访问互联网
                }
            }
        }
        ······
                
    }

效果展示

adb shell am start -n com.android.settings/.Settings
打开原生设置 进入wifi列表可查看效果

原生设置wifi业务连接判断

WifiSettings.java  --- > WifiTracker.java

连接网络:
采用 wifimanager.connect 连接网络

采用 广播WifiManager.NETWORK_STATE_CHANGED_ACTION 监听连接成功


删除网络:
mWifiManager.forget
上一篇下一篇

猜你喜欢

热点阅读