Search code examples
javaandroidcellid

Can not retrieve CellID and LAC for the current cell


I tried to retrieve Cid and Lac for currently connected cell, but using

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();
}

The only thing I get is -1 value for both parameters (I am on 2G). May be I do something wrong or there is another way to get Cid and Lac of current cell?


Solution

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