这个问题可能是由于设备硬件的限制或缺乏权限而导致的。可以尝试使用其他方法来获取基站信息,或者提供必要的权限让getAllCellInfo()返回正确的信息。
以下是一个基于Android 6.0及更高版本的示例代码,可用于获取当前连接的基站信息:
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
CellInfo cellInfo = telephonyManager.getAllCellInfo().get(0);
if (cellInfo instanceof CellInfoGsm) {
CellIdentityGsm cellIdentityGsm = ((CellInfoGsm) cellInfo).getCellIdentity();
Log.d(TAG, "GSM Cell Identity: " + cellIdentityGsm.getCid());
} else if (cellInfo instanceof CellInfoCdma) {
CellIdentityCdma cellIdentityCdma = ((CellInfoCdma) cellInfo).getCellIdentity();
Log.d(TAG, "CDMA Cell Identity: " + cellIdentityCdma.getBasestationId());
} else if (cellInfo instanceof CellInfoLte) {
CellIdentityLte cellIdentityLte = ((CellInfoLte) cellInfo).getCellIdentity();
Log.d(TAG, "LTE Cell Identity: " + cellIdentityLte.getCi());
} else if (cellInfo instanceof CellInfoWcdma) {
CellIdentityWcdma cellIdentityWcdma = ((CellInfoWcdma) cellInfo).getCellIdentity();
Log.d(TAG, "WCDMA Cell Identity: " + cellIdentityWcdma.getCid());
}
请注意,这段代码使用了TelephonyManager.getAllCellInfo()方法来获取所有可用的基站信息,并选择第一个元素来获取当前连接的基站信息。此外,以下是获取必要权限的示例代码: