首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓TelephonyManager getAllCellInfo()刷新速率RSSI

安卓TelephonyManager getAllCellInfo()刷新速率RSSI
EN

Stack Overflow用户
提问于 2016-05-25 15:38:08
回答 1查看 3.4K关注 0票数 3

我目前正在为我的大学研究项目开发一个Android应用程序。这个应用程序应该能够读取附近GSM基站的RSSI级别。我编写了以下函数,该函数成功地读取了附近单元的RSSI级别。我每隔200毫秒调用这个函数,但是RSSI值几乎不会随时间而改变。我怀疑基带处理器每个x都只更新这些值,但是我找不到任何关于刷新速率的信息。有人知道getAllCellInfo()中的信息刷新的速度有多快吗?我之所以想知道这一点,是因为时间对我的安排来说是非常重要的。在实验室里,我们在一定频率(大于1Hz)上启用和禁用干扰器。启用干扰器后,RSSI值将下降。因此,我想知道RSSI能以多快的速度被刷新,以检测这个信号下降,如果干扰机是启用和禁用,例如10赫兹。

代码语言:javascript
复制
public HashMap<Integer,Integer> RSSI_values() {

    HashMap<Integer,Integer> result = new HashMap<Integer,Integer>();

    TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.getNetworkType();
    List<CellInfo> CellInfo_list = telephonyManager.getAllCellInfo();
    if(telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_EDGE) {
        for (int i = 0; i < CellInfo_list.size(); i++) {
            CellInfoGsm cellinfogsm = (CellInfoGsm) CellInfo_list.get(i);

            int cid = cellinfogsm.getCellIdentity().getCid();

            CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
            int rssi = cellSignalStrengthGsm.getDbm();

            result.put(cid,rssi);
        }
    } 
    return result;
}

如果这个刷新速率是特定于设备的,我使用的是nexus 5 android 6.0.1

EN

回答 1

Stack Overflow用户

发布于 2016-05-25 20:06:40

我认为您可以在这个实现上使用更好的方法。

您可以注册一个监听器,而不是使用定义的间隔来查询Cell,每次单元格信息发生变化时都会调用该监听器。

这样,您就不需要担心理想的值,也不必浪费资源来检查可能尚未更改的信息。

您可以使用PhoneStateListener。您创建它并注册它以接收单元信息更改。当它不再需要时(背景中的活动或被破坏的活动),你必须注销它。

下面是一个例子。我没有测试。但它可能会帮助你得到这个想法。

代码语言:javascript
复制
public class MainActivity extends Activity {

    private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
        @Override
        public void onCellInfoChanged(List<CellInfo> cellInfoList) {
            // This callback method will be called automatically by Android OS
            // Every time a cell info changed (if you are registered)
            // Here, you will receive a cellInfoList....
            // Same list that you will receive in RSSI_values() method that you created
            // You can maybe move your whole code to here....
        }
    };

    @Override
    public void onStart() {
        super.onStart();

        // Code below register your mPhoneStateListener will start to be called everytime cell info changed.
        // If you update any UI element, be sure that they were created already (since they are created during "onCreate".. and not at onStart)
        // I added LISTEN_CELL_LOCATION.. But I think that PhoneStateListener.LISTEN_CELL_INFO is enough
        TelephonyManager mTelephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_CELL_INFO);
    }

    @Override
    public void onStop() {
        // Code below unregister your listener... You will not receive any cell info change notification anymore
        TelephonyManager mTelephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
        super.onStop();
    }
}

希望我能帮你。

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

https://stackoverflow.com/questions/37441832

复制
相关文章

相似问题

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