6

我正在尝试获取以下数据:

  • 基站:CellID和RSS(识别哪一个是基站)
  • 对于所有相邻站:CellID 和 RSS

有各种 API,看起来我必须使用不同的 API telephonyManager 和 PhoneStateListener。我有点困惑,因为我认为这应该在一个界面中可用。此外,我认为应该可以轮询当前基站的 CellID,而不必通过监听状态变化来确定 int,因为相邻的基站也可以从 telephonyManager 轮询。

你能告诉我如何获得上面指定的数据吗?

4

1 回答 1

6

新的 skool-way:API 17 更好更干净,但仍然太新。

List<CellInfo> cellInfos = (List<CellInfo>) this.telephonyManager.getAllCellInfo();

for(CellInfo cellInfo : cellInfos)
{
    CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;

    CellIdentityGsm cellIdentity = cellInfoGsm.getCellIdentity();
    CellSignalStrengthGsm cellSignalStrengthGsm = cellInfoGsm.getCellSignalStrength();

    Log.d("cell", "registered: "+cellInfoGsm.isRegistered());
    Log.d("cell", cellIdentity.toString());         
    Log.d("cell", cellSignalStrengthGsm.toString());
}

旧的 API 没有提供非常令人满意的解决方案。但这是老派的方式:

this.telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
this.phoneStateListener = setupPhoneStateListener();
this.telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION);
this.telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
this.telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

// This part is used to listen for properties of the neighboring cells
List<NeighboringCellInfo> neighboringCellInfos = this.telephonyManager.getNeighboringCellInfo();
for(NeighboringCellInfo neighboringCellInfo : neighboringCellInfos)
{
    neighboringCellInfo.getCid();
    neighboringCellInfo.getLac();
    neighboringCellInfo.getPsc();
    neighboringCellInfo.getNetworkType();
    neighboringCellInfo.getRssi();

    Log.d("cellp",neighboringCellInfo.toString());
}

public PhoneStateListener setupPhoneStateListener()
{
    return new PhoneStateListener() {

        /** Callback invoked when device cell location changes. */
        @SuppressLint("NewApi")
        public void onCellLocationChanged(CellLocation location)
        {
            GsmCellLocation gsmCellLocation = (GsmCellLocation) location;
            gsmCellLocation.getCid();
            gsmCellLocation.getLac();
            gsmCellLocation.getPsc();

            Log.d("cellp", "registered: "+gsmCellLocation.toString());
        }

        /** invoked when data connection state changes (only way to get the network type) */
        public void onDataConnectionStateChanged(int state, int networkType)
        {
            Log.d("cellp", "registered: "+networkType);
        }

        /** Callback invoked when network signal strengths changes. */
        public void onSignalStrengthsChanged(SignalStrength signalStrength)
        {
            Log.d("cellp", "registered: "+signalStrength.getGsmSignalStrength());
        }

    };
}
  • 不要忘记设置所有必要的权限这个解决方案的问题是,我没有得到任何相邻的单元格信息,即使我设置了:

    使用权限 android:name="android.permission.ACCESS_COARSE_UPDATES"

(请注意,我使用的是三星手机,三星手机不支持列出相邻电池是一个已知问题)

于 2013-03-23T04:09:49.500 回答