如何在android中获取cell tower位置

时间:2012-03-09 07:15:30

标签: android

我想找到最近的蜂窝塔位置。我试过了

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;
        }
    }
}

我试过这个链接,但它不适用于我How to find user location using cell tower?

我认为我做错了什么。因为这个链接答案正在为其他人工作 我做了链接中显示的相同代码 我使用2,2谷歌API来创建项目 但我无法获得手机信号塔的位置

3 个答案:

答案 0 :(得分:1)

我执行此操作的代码不等待意图,但在活动onCreate中提取对电话服务的引用,并在需要时显示只调用getCellLocation()

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());
    }
}

在侦听PhoneStateService意图时,我也看到我们必须在TelephonyManager上调用listen并指定感兴趣的字段。就我而言:

@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);
}

您可能需要将LISTEN_CELL_LOCATION添加到列表中。

注意:您是否已将ACCESS_COARSE_LOCATION权限添加到应用清单?如PhoneStateListener文档中所述,如果您没有信息权限,某些字段将为空。

答案 1 :(得分:1)

查看此网站,它解释得非常简单:https://www.mylnikov.org/archives/1059

以下是一个例子:

model_dummy <- nls(y ~ (asym+ asym.L1 * is.L1 + asym.l2*is.L2) / 
(1 + exp( -slope * (x – (x0 + x0.L1 * is.L1 + x0.L2 * is.L2) ) ) ), 
start = c(…), data = my_data)

API LINK:https://api.mylnikov.org/geolocation/cell?v=1.1&data=open&mcc=268&mnc=06&lac=8280&cellid=5616

希望它对你有所帮助。

答案 2 :(得分:0)

尝试使用代码 -

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;
    }
}
}

来自StackOverflow

上的这个问题