首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓:为基站和邻近小区获取CellID和RSS

安卓:为基站和邻近小区获取CellID和RSS
EN

Stack Overflow用户
提问于 2013-03-22 17:43:28
回答 1查看 15.3K关注 0票数 6

我试图获得以下数据:

  • 基站: CellID和RSS (识别哪个是基站)
  • 适用于所有邻居站: CellID和RSS

有各种各样的API,看起来我必须使用不同的API,telephonyManager和PhoneStateListener。我有点困惑,因为我认为这应该在一个接口中可用。此外,我认为应该可以投票当前基站的CellID,而不必听状态更改来确定int,因为邻近的小区站cal也是从telephonyManager中进行的。

你能告诉我怎样才能得到上面指定的数据吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-23 04:09:49

新的skool: API 17更好,更干净,但还是太新了.

代码语言:javascript
复制
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并没有提供一个非常令人满意的解决方案。但这是一种古老的斯考尔方式:

代码语言:javascript
复制
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“

(请注意,我使用的是三星手机,这是一个众所周知的问题,三星手机不支持相邻手机的列表)

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15576701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档