Java 无法检索当前单元格的CellID和LAC

Java 无法检索当前单元格的CellID和LAC,java,android,cellid,Java,Android,Cellid,我尝试为当前连接的单元检索Cid和Lac,但使用 public void GetCid(){ int CID; int LAC; GsmCellLocation xXx = new GsmCellLocation(); CID = xXx.getCid(); LAC = xXx.getLac(); Toast output = Toast.makeText(getApplicationContext(), "Base station LAC is "+LAC+"\n"

我尝试为当前连接的单元检索Cid和Lac,但使用

public void GetCid(){
  int CID;
  int LAC;
  GsmCellLocation xXx = new GsmCellLocation();
  CID = xXx.getCid();
  LAC = xXx.getLac();
  Toast output = Toast.makeText(getApplicationContext(), "Base station LAC is "+LAC+"\n" 
  +"Base station CID is " +CID, Toast.LENGTH_SHORT);
  output.show();
}

我在2G上得到的两个参数的唯一值都是-1。可能是我做错了什么,还是有其他方法可以获取当前单元格的Cid和Lac?

谢谢您的快速回复。我只有一个问题。为什么要使用context.getSystemService,而不仅仅是getSystemService?实际上没有区别。我使用上下文字段是因为我不是从活动调用此方法。如果您需要从Activity获取TelephonyManager,请致电getSystemService。感谢您的快速回复。我只有一个问题。为什么要使用context.getSystemService,而不仅仅是getSystemService?实际上没有区别。我使用上下文字段是因为我不是从活动调用此方法。如果需要从Activity获取TelephonyManager,只需调用getSystemService。
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
CellLocation location = telephonyManager.getCellLocation();
GsmCellLocation gsmLocation = (GsmCellLocation) location;
int cellId = gsmLocation.getCid();
int lac = gsmLocation.getLac();