如何在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;
        }
    }
}

我尝试了这个链接,但它对我不起作用。如何使用手机塔找到用户位置?

我想我做错了什么。因为这个链接的答案对其他人有效我做了与链接中所示相同的代码我正在使用2,2谷歌api创建项目但我无法获得蜂窝塔的位置

查看这个网站,它解释得很好,也很简单:https://www.mylnikov.org/archives/1059

这里有一个例子:

.MCC: 268
. MNC: 06
.LAC: 8280
.CELL ID: 5616

API链接:https://api.mylnikov.org/geolocation/cell?v=1.1&数据=打开&mcc=268&mnc=06&lac=8280&cellid=5616

希望它能帮助你。

我拥有的执行此操作的代码不会等待意图,而是在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: unknownn");
    } else {
        out.format("lac: %08xn", 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文档中所述,如果您没有信息权限,则某些字段将为空。

试用该代码-

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

相关内容

  • 没有找到相关文章

最新更新