Reputation: 47
I want to get the cell id and location area code (provided by the network operators) in my android app.
If anyone could help me out with the above.
Upvotes: 0
Views: 924
Reputation: 1412
You should add this Permission in AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
In Java Code,
TelephonyManager telephonyManager = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation gsmCellLocation = (GsmCellLocation)telephonyManager.getCellLocation();
int cid = gsmCellLocation.getCid() & 0xffff; // GSM cell id
int lac = gsmCellLocation.getLac() & 0xffff; // GSM Location Area Code
I have got cell id and the location area code for GSM.
But for UMTS, getCid () returns a big number for example 33 187 589. So i add modulo operator (example: gsmCellLocation.getCid() & 0xffff).
Happy Coding :)
Upvotes: 1
Reputation: 47
This works.
final TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (telephony.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
final GsmCellLocation location = (GsmCellLocation) telephony.getCellLocation();
if (location != null) {
lac.setText("LAC: " + location.getLac() + " CID: " + location.getCid());
}
}
Upvotes: 0