簡體   English   中英

如何在android中獲取cell tower位置

[英]how to get cell tower location in 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 data = open mcc = 268 mc = 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: 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文檔中所述,如果您沒有信息權限,某些字段將為空。

試試這個代碼 -

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上的這個問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM