Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get cell tower location in android

Tags:

android

I want to find nearest cell tower location. 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 is not working for me How to find user location using cell tower?

I think I am doing something wrong. Because this link answer is working for other person I did the same code as shown in link I am using 2,2 google api to create project But I am not able to get cell tower location

like image 558
Dipali Avatar asked Oct 19 '25 12:10

Dipali


2 Answers

Check this site, it's 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 it helped you.

like image 130
Bruno Costa Avatar answered Oct 21 '25 01:10

Bruno Costa


The code I have which does this doesn't wait for the intent but in the activity onCreate fetches a reference to the telephony service and when displaying just calls getCellLocation() when 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());
    }
}

When listening for PhoneStateService intents though I also see that we have to call listen on the TelephonyManager and specify the fields of interest. In my case that 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: Did you add the ACCESS_COARSE_LOCATION permission to the app manifest? As noted in the PhoneStateListener documentation, some fields will be empty if you do not have permission for the information.

like image 29
patthoyts Avatar answered Oct 21 '25 01:10

patthoyts