How to get cell location of a cell in android
I want to find the closest cell. I tried
private class ServiceStateHandler extends Handler {
public void handleMessage(Message msg) {
switch (msg.what) {
case MY_NOTIFICATION_ID:
ServiceState state = mPhoneStateReceiver.getServiceState();
System.out.println(state.getCid());
System.out.println(state.getLac());
System.out.println(mPhoneStateReceiver.getSignalStrength());
break;
}
}
}
I tried this link but it doesn't work for me How to find user location using mesh camera?
I think I am doing something wrong. Since this link answer works for another person I did the same code as in the link I am using 2.2 google api to build the project But I cannot get the cell location of the cell
source to share
The code I have does not wait for intent, but in the onCreate activity retrieves the reference to the telephony service and when displaying only getCellLocation () calls if required.
m_manager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation loc = (GsmCellLocation)m_manager.getCellLocation();
if (loc != null)
{
out.format("Location ");
if (loc.getCid() == -1) {
out.format("cid: unknown ");
} else {
out.format("cid: %08x ", loc.getCid());
}
if (loc.getLac() == -1) {
out.format("lac: unknown\n");
} else {
out.format("lac: %08x\n", loc.getLac());
}
}
While listening to the intents of the PhoneStateService, although I also see that we have to call listen
in the TelephonyManager and specify the fields of interest. In my case it was:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "service start");
m_manager.listen(m_listener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS | PhoneStateListener.LISTEN_SERVICE_STATE | PhoneStateListener.LISTEN_CALL_STATE);
return super.onStartCommand(intent, flags, startId);
}
You probably need to add LISTEN_CELL_LOCATION
to the list.
Note. Have you added the ACCESS_COARSE_LOCATION permission to your app manifest? As stated in the PhoneStateListener documentation, some fields will be empty if you don't have permission for this information.
source to share
Check out this site, it is very well explained and simple: https://www.mylnikov.org/archives/1059
Here's an example:
.MCC: 268
. MNC: 06
.LAC: 8280
.CELL ID: 5616
API LINK: https://api.mylnikov.org/geolocation/cell?v=1.1&data=open&mcc=268&mnc=06&lac=8280&cellid=5616
Hope this helped you.
source to share
Try the code -
mPhoneStateReceiver = new PhoneStateIntentReceiver(this, new ServiceStateHandler());
mPhoneStateReceiver.notifyServiceState(MY_NOTIFICATION_ID);
mPhoneStateReceiver.notifyPhoneCallState(MY_NOTIFICATION_ID);
mPhoneStateReceiver.notifySignalStrength(MY_NOTIFICATION_ID);
mPhoneStateReceiver.registerIntent();
private class ServiceStateHandler extends Handler {
public void handleMessage(Message msg) {
switch (msg.what) {
case MY_NOTIFICATION_ID:
ServiceState state = mPhoneStateReceiver.getServiceState();
System.out.println(state.getCid());
System.out.println(state.getLac());
System.out.println(mPhoneStateReceiver.getSignalStrength());
break;
}
}
}
From this question at fooobar.com/questions/522199 / ...
source to share