views:

29

answers:

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?

+1  A: 
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
Thanks for a fast reply. I have just one question. Why do u use context.getSystemService, but not just getSystemService?
StalkerRus
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
Tnx for explanation.
StalkerRus