Search code examples
androidcellular-networklte

Why I'm Not allowed to access cell info


I'm trying to get information about cellular network (neighbour cells, signal strength, etc.) but I faced the problem with one mwthod (telephonyManager.getAllCellInfo()). Tested on OnePlus 6t.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.text);
        telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, READ_SMS) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, READ_PHONE_NUMBERS) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{READ_SMS, READ_PHONE_NUMBERS, READ_PHONE_STATE,ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_CODE);
        } else {
            textView.setText(""+telephonyManager.getAllCellInfo());
        }
    }

In LogCat I see:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cellularanalyzer/com.example.cellularanalyzer.MainActivity}: java.lang.SecurityException: Not allowed to access cell info

In case I change telephonyManager.getAllCellInfo() to telephonyManager.getSignalStrength() I get some information, but not all of it.


Solution

  • You need the ACCESS_FINE_LOCATION runtime permission in order to get information from getAllCellInfo.

    Source: https://developer.android.com/reference/android/telephony/TelephonyManager

    The information from the method allows developers to estimate the location of the user.