2012-03-09 69 views
3

我想查找最近的細胞塔位置。我試圖如何在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創建項目 但我沒能獲得手機信號塔位置

+0

檢出此鏈接http://stackoverflow.com/a/3145655/28557,它很好地解釋瞭如何獲得當前位置 – 2012-03-09 07:40:36

+0

可能重複[獲取單元格塔位置 - Android](http://stackoverflow.com/questions/6668271/get-cell-tower-locations-android) – 2012-03-10 07:16:11

回答

0

試用THI代碼 -

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

+0

我試過這個,我在我的問題中提到過它。 phonestatereceiver無法正常工作 – Dipali 2012-03-09 08:29:16

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文檔中所述,如果您沒有信息權限,則某些字段將爲空。

+0

我想查找手機信號塔的位置(地址)。 我有mcc mnc cell id和lac .. 你知道如何找到它嗎? – Dipali 2012-03-09 08:27:42