I want to display the cell location, id, and area code. However, when I run the application, it gives me a runtime error. From debug perspective I see that the GsmCellLocation object produces a null value. (cellLocation == null if I debug)
Here’s my sample code:
TelephonyManager teleplonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation)teleplonyManager.getCellLocation();
int cid = cellLocation.getCid();
int lac = cellLocation.getLac();
textGSMCellLocation.setText(cellLocation.toString());
textCID.setText("gsm cell id : "+String.valueOf(cid));
textLAC.setText("gsm location area code : "+String.valueOf(lac));
this is my menifest.xml
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
In The Docs it says:
To reiterate: It’s returning null because the current location is not available.
Maybe the the location providers are disabled or it simply cannot find the location.
In any case, if you’re needing location, you would be better off using
LocationManagerbecause it is designed for location updates and has much more useful methods thanTelephonyManager.getCellLocation();