如何在Android设备中获取基站位置信息

4
我想找到最近的手机信号塔位置。我尝试过:
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;
        }
    }
}

我尝试使用此链接,但对我无效如何使用蜂塔找到用户位置?

我认为我做错了什么,因为这个链接的答案适用于其他人 我使用与链接中相同的代码 我正在使用2.2谷歌API创建项目 但我无法获取蜂塔位置


请查看此链接https://dev59.com/QnA75IYBdhLWcg3wuLjD#3145655,其中详细解释了如何获取当前位置。 - Vinayak Bevinakatti
可能是获取手机信号塔位置-Android的重复问题。 - Jim Garrison
3个回答

2

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文档中所述,如果没有信息的权限,某些字段将为空。


我想找到手机信号塔的位置(地址)。我有MCC、MNC、Cell ID和LAC。你知道如何找到吗? - Dipali

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上的这个问题开始


我尝试过这个并在我的问题中提到了它。phonestatereceiver没有起作用。 - Dipali

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接