3

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

Community
  • 1
  • 1
Dipali
  • 1,148
  • 3
  • 13
  • 23

3 Answers3

2

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.

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
1

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.

patthoyts
  • 32,320
  • 3
  • 62
  • 93
  • I want to find location (Address) of cell tower. I have mcc mnc cell id and lac.. do u know how to find that? – Dipali Mar 09 '12 at 08:27
0

Try out thi 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 on StackOverflow

Community
  • 1
  • 1
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173