1

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
damned
  • 9
  • 3
  • set targetsdk version to 21 and give permission of access location in manifest. Your problem will be solved. – PRATEEK BHARDWAJ Jan 04 '18 at 10:22
  • Hi Prateek, thanks. That worked. Could you explain what downgrading the targetsdk version does? – damned Jan 04 '18 at 10:39
  • set target sdk version 21 in build.gradle not min sdk version. – PRATEEK BHARDWAJ Jan 04 '18 at 10:40
  • if you set target sdk as 21 then you dont need to use concept of runtime permissions. it gives permission by default same like device below to SDK 23. – PRATEEK BHARDWAJ Jan 04 '18 at 14:06
  • Hey, we still struggling to get this working. We set target SDk to 21 and we are getting very rarely a cellList returned for getallcellinfo(). But in 95% no List of cellInfos is returend. But a phonecall can be place. So there must be a cell connection. – damned Jan 18 '18 at 10:25

1 Answers1

5

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.

Anand
  • 51
  • 6
  • Running the app in foreground is tricky, as it is suppose to run as a service in background and check curretn cell towers when e.g. calls are placed. Location Services are disabled for test devices with Android 7.1 and lower and it works. Is this also only for 8.0 and above? – damned Feb 12 '18 at 15:58
  • Yes. Location Service should be on only for androd 8.0 and above. Below android 8.0 it will work without Location Service. – Anand Feb 13 '18 at 04:58
  • @Anand I do face a similar issue. I see google mentioned same for Android P here - https://developer.android.com/about/versions/pie/android-9.0-changes-all#telephony_information_now_relies_on_device_location_setting However, I did not find any documentation for Android O. Did you see any document? Can you please help me with the link? – Swati Apr 23 '19 at 10:04
  • @Swati, I don't have any link about it. But i do came to know about this while developing and testing on various devices of Android 8.0. – Anand Aug 09 '19 at 06:13
  • This was the right solution to my problem. Thanks ! – Ramtin Mousavi Jun 15 '20 at 21:55