我正在将我的应用程序的支持升级到 Android Q,但是从更新的 SDK 中,TelephonyManager 类中没有像 getNeighboringCellInfo() 这样的方法https://developer.android.com/reference/android/telephony/TelephonyManager.html有什么办法吗获取相邻小区信息?
谢谢
我正在将我的应用程序的支持升级到 Android Q,但是从更新的 SDK 中,TelephonyManager 类中没有像 getNeighboringCellInfo() 这样的方法https://developer.android.com/reference/android/telephony/TelephonyManager.html有什么办法吗获取相邻小区信息?
谢谢
是的,在 sdk 29 之后,getNeighboringCellInfo
已弃用。
它被替换为getAllCellInfo
。
使用示例如下:
TelephonyManager manager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
List<CellInfo> cellInfoList = new ArrayList<>();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return cellInfoList;
}
cellInfoList = manager.getAllCellInfo();
if (cellInfoList != null && cellInfoList.size() > 0) {
for (CellInfo info : cellInfoList) {
//
}
}
此外,还有其他工具方法可以获取参数CellInfo
。
如果需要,您可以参考它们。
public static int neighboringCellInfoGetLac(CellInfo cellInfo) {
int lac = 0;
try {
if (VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (cellInfo instanceof CellInfoGsm) {
CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
lac = cellInfoGsm.getCellIdentity().getLac();
} else if (cellInfo instanceof CellInfoLte) {
CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;
lac = cellInfoLte.getCellIdentity().getTac();
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2
&& cellInfo instanceof CellInfoWcdma) {
CellInfoWcdma cellInfoLte = (CellInfoWcdma) cellInfo;
lac = cellInfoLte.getCellIdentity().getLac();
} else if (cellInfo instanceof CellInfoCdma) {
CellInfoCdma cellInfoCdma = (CellInfoCdma) cellInfo;
lac = cellInfoCdma.getCellIdentity().getNetworkId();
} else if (cellInfo instanceof CellInfoTdscdma) {
CellInfoTdscdma cellInfoTdscdma = (CellInfoTdscdma) cellInfo;
lac = cellInfoTdscdma.getCellIdentity().getLac();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return lac;
}
public static int neighboringCellInfoGetCid(CellInfo cellInfo) {
int cid = 0;
try {
if (VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (cellInfo instanceof CellInfoGsm) {
CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
cid = cellInfoGsm.getCellIdentity().getCid();
} else if (cellInfo instanceof CellInfoLte) {
CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;
cid = cellInfoLte.getCellIdentity().getCi();
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2
&& cellInfo instanceof CellInfoWcdma) {
CellInfoWcdma cellInfoLte = (CellInfoWcdma) cellInfo;
cid = cellInfoLte.getCellIdentity().getCid();
} else if (cellInfo instanceof CellInfoCdma) {
CellInfoCdma cellInfoCdma = (CellInfoCdma) cellInfo;
cid = cellInfoCdma.getCellIdentity().getBasestationId();
} else if (cellInfo instanceof CellInfoTdscdma) {
CellInfoTdscdma cellInfoTdscdma = (CellInfoTdscdma) cellInfo;
cid = cellInfoTdscdma.getCellIdentity().getCid();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return cid;
}
public static int neighboringCellInfoGetRssi(CellInfo cellInfo) {
int rssi = 0;
try {
if (VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (cellInfo instanceof CellInfoGsm) {
CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
rssi = cellInfoGsm.getCellSignalStrength().getDbm();
} else if (cellInfo instanceof CellInfoLte) {
CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;
rssi = cellInfoLte.getCellSignalStrength().getRssi();
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2
&& cellInfo instanceof CellInfoWcdma) {
CellInfoWcdma cellInfoLte = (CellInfoWcdma) cellInfo;
rssi = cellInfoLte.getCellSignalStrength().getDbm();
} else if (cellInfo instanceof CellInfoCdma) {
CellInfoCdma cellInfoCdma = (CellInfoCdma) cellInfo;
rssi = cellInfoCdma.getCellSignalStrength().getDbm();
} else if (cellInfo instanceof CellInfoTdscdma) {
CellInfoTdscdma cellInfoTdscdma = (CellInfoTdscdma) cellInfo;
rssi = cellInfoTdscdma.getCellSignalStrength().getDbm();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return rssi;
}
getNeighboringCellInfo
现在已弃用,并已替换为getAllCellInfo
.
该方法返回一个列表List<CellInfo>