3

当我请求 Cell ID 和 LAC 信息时,在某些设备上我无法检索它们。

我使用这段代码:

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

cellID = location.getCid();

lac = location.getLac();
  1. 有谁知道为什么有些 GSM 运营商不提供它们?
  2. 我需要权限吗?
  3. 关于检索 CellID 和 LAC 还有什么要了解的?
4

5 回答 5

19

为了找到 CellId,您应该使用 0xffff 作为位掩码,而不是 mod。

错误的

new_cid = cellLocation.getCid() % 0xffff;

new_cid = cellLocation.getCid() & 0xffff;
于 2012-10-19T07:45:24.597 回答
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);

文档所述,LISTEN_CELL_LOCATION 要求您添加以下权限:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
于 2012-04-02T14:44:07.427 回答
0

我认为这是由于制造商在设备上实现底层内核代码的方式,不允许您访问某些信息。

于 2012-03-21T16:07:20.907 回答
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" />
于 2014-01-31T11:48:26.577 回答
-2

所以你可以尝试类似的东西。我有 GSM 的小区 ID 和位置区号。但是对于 UMTS,getCid () 为 exple 33 166 248 返回一个大数字。所以我添加了模运算符 (exple xXx.getCid() % 0xffff)。

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

    new_cid = cellLocation.getCid() % 0xffff;
    new_lac = cellLocation.getLac() % 0xffff;
于 2012-03-30T14:45:45.003 回答