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

Sta*_*Rus 1 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" 
  +"Base station CID is " +CID, Toast.LENGTH_SHORT);
  output.show();
}
Run Code Online (Sandbox Code Playgroud)

我唯一得到的是两个参数的-1值(我在2G上).可能是我做错了什么还是有另一种方法来获得当前细胞的Cid和Lac?

cem*_*ent 5

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

  • 实际上没有区别.我正在使用上下文字段,因为我正在调用此方法而不是来自Activity.如果您需要从Activity获取TelephonyManager,请调用getSystemService. (3认同)