2012-03-21 36 views
3

當我請求Cell ID和LAC信息時,在某些設備上我無法檢索它們。Android:CellID不適用於所有運營商?

我用這個代碼:

TelephonyManager tm =(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
location = (GsmCellLocation) tm.getCellLocation(); 

cellID = location.getCid(); 

lac = location.getLac(); 
  1. 有誰知道爲什麼有些GSM運營商不提供呢?
  2. 我需要權限嗎?
  3. 還有什麼知道關於retreiving CellID和LAC?

回答

-2

所以你可以嘗試類似的東西。我有手機號碼和GSM的位置區號。但對於UMTS,getCid()會返回一個大數字,例如33 166 248.因此,我添加了模運算符(例如xXx.getCid()%0xffff)。

GsmCellLocation cellLocation = (GsmCellLocation)telm.getCellLocation(); 

    new_cid = cellLocation.getCid() % 0xffff; 
    new_lac = cellLocation.getLac() % 0xffff; 
+2

這是錯誤的。 @ nkout的答案是正確的答案。 – 2015-04-16 23:20:09

0

我想這是由於製造商在設備上實現了底層內核代碼的方式,而不允許您訪問某些信息。

2

嘗試使用PhoneStateListener如下:

首先,創建監聽器。

public PhoneStateListener phoneStateListener = new PhoneStateListener() { 
    @Override 
    public void onCellLocationChanged (CellLocation location) { 
     StringBuffer str = new StringBuffer(); 
     // GSM 
     if (location instanceof GsmCellLocation) { 
      GsmCellLocation loc = (GsmCellLocation) location; 
      str.append("gsm "); 
      str.append(loc.getCid()); 
      str.append(" "); 
      str.append(loc.getLac()); 
      Log.d(TAG, str.toString()); 
      } 
    } 
}; 

,然後註冊,上的onCreate(),聽者如下:

telephonyManager = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE); 
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION); 

由於在documentation所述,LISTEN_CELL_LOCATION要求您添加以下權限:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
+0

什麼是CDMA解決方案?當用戶位置服務(設置)關閉時它工作嗎? – 2016-02-10 20:42:23

+0

@guidomocha,解決方案是類似的,但CDMA系統不包含LAC,CID,而是具有網絡ID和系統ID。檢查http://developer.android.com/reference/android/telephony/cdma/CdmaCellLocation.html – Eduardo 2016-02-13 16:39:29

17

爲了找到CellId,你應該使用0xffff作爲位掩碼,而不是mod。

WRONG

new_cid = cellLocation.getCid() % 0xffff; 

RIGHT

new_cid = cellLocation.getCid() & 0xffff; 
+1

正確的文檔。這應該被標記爲答案。 – CodeWarrior 2014-08-28 10:03:50

+0

換句話說,cellLocation.getCid()%65536也應該有效。 – 2015-04-16 23:18:51

0

您需要使用TelephonyManager

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
    GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager 
      .getCellLocation(); 

    // Cell Id, LAC 
    int cellid = cellLocation.getCid(); 
    int lac = cellLocation.getLac(); 

    // MCC 
    String MCC = telephonyManager.getNetworkOperator(); 
    int mcc = Integer.parseInt(MCC.substring(0, 3)); 

    // Operator name 
    String operatoprName = telephonyManager.getNetworkOperatorName(); 

對於許可,您需要添加跟隨着在Manifest.xml文件

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
+0

什麼是CDMA解決方案?當用戶位置服務(設置)關閉時它工作嗎? – 2016-02-10 20:42:55