2012-03-21 75 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