1

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?

StalkerRus
  • 411
  • 1
  • 10
  • 20

1 Answers1

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();
cement
  • 2,905
  • 1
  • 23
  • 20
  • Thanks for a fast reply. I have just one question. Why do u use context.getSystemService, but not just getSystemService? – StalkerRus Oct 07 '10 at 17:11
  • 3
    Actually there is no difference. I'm using context field because I'm calling this method not from Activity. If you need to get TelephonyManager from Activity just call getSystemService. – cement Oct 07 '10 at 17:18