我发现我们有CDMACellLocation和GSMCellLocation的类,但是没有专门针对LTE的。我怎样才能获得与我的电话服务上下文相关的LTE小区位置呢? 谢谢!
MobileInfoRecognizer mobileInfoRecognizer = new MobileInfoRecognizer();
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
List<CellInfo> cellInfos = tm.getAllCellInfo();
additional_info = mobileInfoRecognizer.getCellInfo(cellInfos.get(0));
在我的应用程序中,我发现你只能获取列表中的第一个元素,以下是我的自定义类代码:
public class MobileInfoRecognizer {
public String getCellInfo(CellInfo cellInfo) {
String additional_info;
if (cellInfo instanceof CellInfoGsm) {
CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
CellIdentityGsm cellIdentityGsm = cellInfoGsm.getCellIdentity();
additional_info = "cell identity " + cellIdentityGsm.getCid() + "\n"
+ "Mobile country code " + cellIdentityGsm.getMcc() + "\n"
+ "Mobile network code " + cellIdentityGsm.getMnc() + "\n"
+ "local area " + cellIdentityGsm.getLac() + "\n";
} else if (cellInfo instanceof CellInfoLte) {
CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;
CellIdentityLte cellIdentityLte = cellInfoLte.getCellIdentity();
additional_info = "cell identity " + cellIdentityLte.getCi() + "\n"
+ "Mobile country code " + cellIdentityLte.getMcc() + "\n"
+ "Mobile network code " + cellIdentityLte.getMnc() + "\n"
+ "physical cell " + cellIdentityLte.getPci() + "\n"
+ "Tracking area code " + cellIdentityLte.getTac() + "\n";
} else if (cellInfo instanceof CellInfoWcdma){
CellInfoWcdma cellInfoWcdma = (CellInfoWcdma) cellInfo;
CellIdentityWcdma cellIdentityWcdma = cellInfoWcdma.getCellIdentity();
additional_info = "cell identity " + cellIdentityWcdma.getCid() + "\n"
+ "Mobile country code " + cellIdentityWcdma.getMcc() + "\n"
+ "Mobile network code " + cellIdentityWcdma.getMnc() + "\n"
+ "local area " + cellIdentityWcdma.getLac() + "\n";
}
return additional_info;
}
}
在这里要小心——虽然API可能提供单元格信息的接口,但它实际上取决于运营商。与可以作为其RADIUS输出的一部分提供粗略位置信息的CDMA不同,LTE因实现而异。只有LTE网络的“较低”级别知道您可能在哪里,而且这最多也是模糊的。如果不了解您的运营商基础设施、他们的MME工作方式等等,您可能会获得信息,但我不会相信它用于地理位置信息。
此外,这还取决于您的运营商如何轮询。根据设备配置文件的不同,您可能每分钟轮询一次、每五分钟轮询一次、每两个小时轮询一次。如果您正在漫游,您可能什么都得不到,因为没有关于值的良好标准。
您可以通过迭代getAllCellInfo()返回的List列表,并检查CellInfo是否为CellInfoLte来进行操作。
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
List<CellInfo> cellInfoList = tm.getAllCellInfo();
for (CellInfo cellInfo : cellInfoList)
{
if (cellInfo instanceof CellInfoLte)
{
// cast to CellInfoLte and call all the CellInfoLte methods you need
}
}