简体   繁体   中英

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

I was trying to check if the device is connected to internet and what is the network type. Here is an example of how we check it in 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;
    }

Add permission ohos.permission.GET_NETWORK_INFO, which is used to obtain network information. Add permission ohos.permission.INTERNET, which is used to access the network.

• Check if network is connected

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

• Check if WiFi is connected

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);
}

• Check if Mobile network is connected

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);
    }

You could refer to this Docs to check the status of the connection to the Internet.

And the TelephonyConstants.NETWORK_TYPE_LTE can determine the network type.

// 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM