Reputation: 548
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>
Upvotes: 0
Views: 1891
Reputation: 14984
In The Docs it says:
Returns the current location of the device. Return null if current location is not available.
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 LocationManager
because it is designed for location updates and has much more useful methods than TelephonyManager.getCellLocation();
Upvotes: 1