首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用CellSignalStrengthGsm只给出Integer.MAX_VALUE

使用CellSignalStrengthGsm只给出Integer.MAX_VALUE
EN

Stack Overflow用户
提问于 2013-11-07 15:54:47
回答 1查看 2.8K关注 0票数 3

在看到question和答案(顺便说一句)之后,我编写了这段代码,与答案中的基本相同:

代码语言:javascript
复制
try {
        List<CellInfo> cellInfoList = telephonyManager.getAllCellInfo();
        for (CellInfo cellInfo : cellInfoList) {
            if (cellInfo instanceof CellInfoGsm) {
                CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
                CellIdentityGsm cellIdentity = cellInfoGsm.getCellIdentity();
                final CellSignalStrengthGsm gsm = ((CellInfoGsm) cellInfo).getCellSignalStrength();
                rsrpValue = gsm.getDbm();
                pciValue = cellIdentity.getCid();
            } else if (cellInfo instanceof CellInfoCdma) {
                CellInfoCdma cellInfoCdma = (CellInfoCdma) cellInfo;
                CellIdentityCdma cellIdentity = cellInfoCdma.getCellIdentity();
                pciValue = cellIdentity.getBasestationId();
            } else if (cellInfo instanceof CellInfoLte){
                    CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;
                    CellIdentityLte cellIdentity = cellInfoLte.getCellIdentity();
                    pciValue = cellIdentity.getPci();
            } else {
                throw new Exception("Unknown type of cell signal!");
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Unable to obtain cell signal information", e);
    }

但是当我为GSM显示rsrpValue或pciValue时,我总是得到最大整数值(2147483647)。我用API 17在手机上试了一下。我的代码有什么问题吗?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-19 17:11:55

尝试使用好的老PhoneStateListener来监听信号强度的变化。这就是对我有用的东西。使用此选项注册侦听器:

代码语言:javascript
复制
private TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int currentSignalStrength = 0;
int asu = 0;

telManager.listen(new SignalStrengthListener(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

这是真正的听者:

代码语言:javascript
复制
private class SignalStrengthListener extends PhoneStateListener{

    @Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength)
        {
            super.onSignalStrengthsChanged(signalStrength);
            if (telManager.getPhoneType()== TelephonyManager.PHONE_TYPE_CDMA)
                currentSignalStrength = signalStrength.getCdmaDbm();                  
            else
                asu = signalStrength.getGsmSignalStrength();
        }
    }

在使用CellInfoGsm存在相同问题的Xperia上进行了测试。

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

https://stackoverflow.com/questions/19840487

复制
相关文章

相似问题

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