为什么不允许我访问手机信息

Why I'm Not allowed to access cell info

我正在尝试获取有关蜂窝网络的信息(相邻小区、信号强度等),但我遇到了一个方法 (telephonyManager.getAllCellInfo()) 的问题。 在 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());
        }
    }

在LogCat中我看到:

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

如果我将 telephonyManager.getAllCellInfo() 更改为 telephonyManager.getSignalStrength(),我会得到一些信息,但不是全部。

您需要 ACCESS_FINE_LOCATION 运行时权限才能从 getAllCellInfo 获取信息。

来源:https://developer.android.com/reference/android/telephony/TelephonyManager

来自该方法的信息允许开发人员估计用户的位置。