0

I have this fuction. But i get error on this line:|

int nrState = regInfo.getNrState();

Message:

Cannot resolve method 'getNrState' in 'NetworkRegistrationInfo

Can someone where i make mistake? Or I should do this in differend way?

ServiceState serviceState = telephonyManager.getServiceState();
if (serviceState != null) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        for (NetworkRegistrationInfo regInfo : serviceState.getNetworkRegistrationInfoList()) {
            if (regInfo.getAccessNetworkTechnology() == TelephonyManager.NETWORK_TYPE_NR) {
                int nrState = regInfo.getNrState(); // 👈
                switch (nrState) {
                    case NetworkRegistrationInfo.NR_STATE_NONE:
                        return "Not in NR state";
                    case NetworkRegistrationInfo.NR_STATE_RESTRICTED:
                        return "Restricted NR state";
                    case NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED:
                        return "Non-restricted NR state";
                    case NetworkRegistrationInfo.NR_STATE_CONNECTED:
                        return "Connected to 5G NR";
                }
            }
        }
    }
}

I need access to getNrState() for each sim card.

1 Answer 1

0

The problem you are facing is beacuse getNrState() was introduced in API 29. If you are using any device or emulater below this API level then the function is not available. Moreover many devices might not have full 5G support and hence the method be unrecognised in some devices.

Here's what you can do :

ServiceState serviceState = telephonyManager.getServiceState();
 if (serviceState != null) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { 

           // Ensure Android 11 or higher        
            List<NetworkRegistrationInfo> regInfoList = serviceState.getNetworkRegistrationInfoList();        
            if (regInfoList != null) {
            for (NetworkRegistrationInfo regInfo : regInfoList) {
                if (regInfo.getAccessNetworkTechnology() == TelephonyManager.NETWORK_TYPE_NR) {

                    // Ensure API level compatibility for getNrState()
                      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                        int nrState = regInfo.getNrState(); // Only available on API 29+switch (nrState) {
                            case NetworkRegistrationInfo.NR_STATE_NONE:
                                return "Not in NR state";
                            case NetworkRegistrationInfo.NR_STATE_RESTRICTED:
                                return "Restricted NR state";
                            case NetworkRegistrationInfo.NR_STATE_NOT_RESTRICTED:
                                return "Non-restricted NR state";
                            case NetworkRegistrationInfo.NR_STATE_CONNECTED:
                                return "Connected to 5G NR";
                        }
                    } else {
                        return "getNrState() not available on this Android version";
                    }
                }
            }
        }
    }
}
1
  • Hi. Thx for reply. I understand that this function will not work on all devices. By my problem is that I can`t compile this function. While running app I get. "Cannot resolve method 'getNrState' in 'NetworkRegistrationInfo'". Its look like this method dont exist in this object. But I see that in this class that method exist. This is why i confused.
    – Robert O
    Commented 20 mins ago

Not the answer you're looking for? Browse other questions tagged or ask your own question.