How to get Cell ID and Location Area code in android 9 ( Pie ) devices without using GPS?
Telephony information now relies on device location setting
If the user has disabled device location on a device running Android 9, the following methods don't provide results:
getAllCellInfo()
listen()
getCellLocation()
getNeighboringCellInfo()
Android : Finding location using LAC and CID
This one way to get location through LAC and CID:
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.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));
if(RqsLocation(cid, lac)){
textGeo.setText(
String.valueOf((float)myLatitude/1000000)
+ " : "
+ String.valueOf((float)myLongitude/1000000));
latitude=String.valueOf((float)myLatitude/1000000);
longitude=String.valueOf((float)myLongitude/1000000);
lat_double=Double.parseDouble(latitude);
lang_double=Double.parseDouble(longitude);
geocoder = new Geocoder(AndroidTelephonyManager.this, Locale.ENGLISH);
try {
addresses = geocoder.getFromLocation(lat_double, lang_double, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuilder str = new StringBuilder();
//if (geocoder.isPresent()) {
// Toast.makeText(getApplicationContext(),
// "geocoder present",
// Toast.LENGTH_SHORT).show();
Address returnAddress = addresses.get(0);
String area = returnAddress.getFeatureName();
String thfare = returnAddress.getThoroughfare();
String localityString = returnAddress.getLocality();
// String region_code = returnAddress.getCountryCode();
String zipcode = returnAddress.getPostalCode();
String state = returnAddress.getAdminArea();
String sublocal = returnAddress.getSubLocality();
String city = returnAddress.getCountryName();
Toast.makeText(getApplicationContext(), latitude, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), longitude, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), thfare, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), area, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), localityString, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), zipcode, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), sublocal, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), state, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), city, Toast.LENGTH_LONG).show();
}else{
textGeo.setText("Can't find Location!");
};
// }
}
private Boolean RqsLocation(int cid, int lac){
Boolean result = false;
String urlmmap = "http://www.google.com/glm/mmap";
try {
URL url = new URL(urlmmap);
URLConnection conn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.connect();
OutputStream outputStream = httpConn.getOutputStream();
WriteData(outputStream, cid, lac);
InputStream inputStream = httpConn.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
dataInputStream.readShort();
dataInputStream.readByte();
int code = dataInputStream.readInt();
if (code == 0) {
myLatitude = dataInputStream.readInt();
myLongitude = dataInputStream.readInt();
result = true;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
private void WriteData(OutputStream out, int cid, int lac)
throws IOException
{
DataOutputStream dataOutputStream = new DataOutputStream(out);
dataOutputStream.writeShort(21);
dataOutputStream.writeLong(0);
dataOutputStream.writeUTF("en");
dataOutputStream.writeUTF("Android");
dataOutputStream.writeUTF("1.0");
dataOutputStream.writeUTF("Web");
dataOutputStream.writeByte(27);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(3);
dataOutputStream.writeUTF("");
dataOutputStream.writeInt(cid);
dataOutputStream.writeInt(lac);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.writeInt(0);
dataOutputStream.flush();
}
}
How to find GSM Location area code and Cell Id for iPhone
You can't find the LAC and Cell Id, but you can find the approximate location using CoreLocation. You can then use this to get an approximate area code if you have a suitable mapping of geo-locations to area codes; but it's unlikely that you'll be able to find a perfect mapping in any case, even if you knew the Cell Id.
The documentation for Core Location is here:
http://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CoreLocation_Framework/
How to find short cell ID?
It appears that "short cell ID" is nothing but the lower 16 bits of the Long cell ID.
So I used:
int cid = cl.getCid();
short cid_short = (short) cid;
Related Topics
How to Capitalize the First Letter of Text in a Textview in an Android Application
Update Some Specific Field of an Entity in Android Room
How to Check Miui Autostart Permission Programmatically
How to Make a New Line or Tab in <String> Xml (Eclipse/Android)
Open External Links in the Browser With Android Webview
How to Avoid Keyboard Pushing Layout Up on Android React-Native
How to Center the Elements in Constraintlayout
Scrollview Not Scrolling in Android Bottomsheet
Check If Json Object Contains Specific Value
How to Get the Currently Displayed Fragment
How to Figure Out Which Sim Received Sms in Dual Sim Android Device
Onclick() to Change Button Border Color Rather Changing Button Color
Remove Span Tag With Text from String
Touchableopacity Not Working Inside an Absolute Positioned View