getAllCellInfo() returns empty list on Nexus 5x

2.1k Views Asked by At

I read all posts about getAllCellInfo() and have a similar situation like this. The code posted below runs well on Android 6 (OnePlus One) but returns an empty list of Cellinfo on Google Nexus 5x Android 8.1 API 27. I have set the Permission ACCESS CORSE LOCATION in the manifest and asking for permission when running the app first time.

Here is a code snippet:

try {

        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        String networkOperator = "";
        if (tm.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) { 
            networkOperator = tm.getNetworkOperator(); 
        }

        if (!TextUtils.isEmpty(networkOperator)) { //is not empty with Nexus 5x
            mcc = networkOperator.substring(0, 3); 
            mnc = networkOperator.substring(3);
        } else {
            mcc = "Unbekannt";
            mnc = "Unbekannt";
        }

        List<CellInfo> cell = tm.getAllCellInfo();
        System.out.println("List: "+cell);
        if (cell != null) { //Always null with Nexus 5x
          //  TODO
        } else {
            cellID = "ERROR";
            lac = "ERROR";
        }
    }
    catch(Exception ex) {
        mcc = "No Permission";
        mnc = "No Permission";
        cellID = "ERROR";
        lac = "ERROR";
    }
}

When trying other apps like "Network Cell Info lite" these apps get all the cellinformation :(

Any Help is very appreciated.

1

There are 1 best solutions below

5
On

In Android 8.0 and above,to access getAllCellInfo() your app should be in the foreground.

If you want to access getAllCellInfo() you need to enable Location Service too.