如何检查设备是否已连接到互联网以及 Harmony 中的连接类型 Os

How to check if the device is connected to internet and Connection type in Harmony Os

我正在尝试检查设备是否已连接到互联网以及网络类型是什么。这是我们如何在 android

中检查它的示例
public boolean isConnectingToInternet(Activity act){
        boolean isthere = false;
        TelephonyManager tm = (TelephonyManager) act.getSystemService(Context.TELEPHONY_SERVICE); 
        if (tm.getSimState() != TelephonyManager.SIM_STATE_UNKNOWN){
            ConnectivityManager connectivityManager = (ConnectivityManager) act.getSystemService(Context.CONNECTIVITY_SERVICE);
            if ((connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED))
                isthere = true;
        } else {
            ConnectivityManager connectivityManager = (ConnectivityManager) act.getSystemService(Context.CONNECTIVITY_SERVICE);
            if ((connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED))
                isthere = true;
        }
        return isthere;
    }

添加权限ohos.permission.GET_NETWORK_INFO,用于获取网络信息。添加权限ohos.permission.INTERNET,用于访问网络

• 检查网络是否已连接

public static boolean hasInternetConnection(Context context) {
        NetManager netManager = NetManager.getInstance(context);
        NetCapabilities netCapabilities = netManager.getNetCapabilities(netManager.getDefaultNet());
        return netCapabilities.hasCap(NetCapabilities.NET_CAPABILITY_VALIDATED);
}

• 检查 WiFi 是否已连接

public static boolean isWifiConnectedInternet(Context context) {
       NetManager netManager = NetManager.getInstance(context);
       NetCapabilities netCapabilities = netManager.getNetCapabilities(netManager.getDefaultNet());
       return netCapabilities.hasCap(NetCapabilities.NET_CAPABILITY_VALIDATED) &&
                netCapabilities.hasBearer(NetCapabilities.BEARER_WIFI) ||
                netCapabilities.hasBearer(NetCapabilities.BEARER_WIFI_AWARE);
}

• 检查移动网络是否已连接

public static boolean isMobileConnectedInternet(Context context) {
        NetManager netManager = NetManager.getInstance(context);
        NetCapabilities netCapabilities = netManager.getNetCapabilities(netManager.getDefaultNet());
        return netCapabilities.hasCap(NetCapabilities.NET_CAPABILITY_VALIDATED) &&
                netCapabilities.hasBearer(NetCapabilities.BEARER_CELLULAR);
    }

您可以参考此Docs检查互联网连接状态。

TelephonyConstants.NETWORK_TYPE_LTE可以判断网络类型

// Obtain the RadioInfoManager object.
RadioInfoManager radioInfoManager = RadioInfoManager.getInstance(context);

// Obtain the signal information.
List<SignalInformation> signalList = radioInfoManager.getSignalInfoList(slotId);

// Check the size of the signal information list.
if (signalList.size() == 0) {
    return;
}
// Traverse the signal information list to obtain the signal information of the current network type.
LteSignalInformation lteSignal = null;
for (SignalInformation signal : signalList) {
    int signalNetworkType = signal.getNetworkType();
    if (signalNetworkType == TelephonyConstants.NETWORK_TYPE_LTE) {
        lteSignal = (LteSignalInformation) signal;
    }
}
// Obtain the signal strength of the specified RAT (methods in the child class).
int signalLevel = lteSignal != null ? lteSignal.getSignalLevel() : 0;