我尝试检索当前连接的单元格的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();
}


我唯一得到的是两个参数均为-1值(我使用2G)。可能是我做错了什么,还是有另一种方法来获取当前单元格的Cid和Lac?

最佳答案

TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
CellLocation location = telephonyManager.getCellLocation();
GsmCellLocation gsmLocation = (GsmCellLocation) location;
int cellId = gsmLocation.getCid();
int lac = gsmLocation.getLac();

10-07 23:18