Domanda

after seeing that question and the answers (thanks by the way) I wrote this code wich is pretty much the same as in the answers:

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);
    }

But when I display rsrpValue or pciValue for GSM, I always get the maximum integer value (2147483647). I tried this on a phone with the API 17. Is there something wrong in my code?

Thanks

È stato utile?

Soluzione

Try to use the good old PhoneStateListener to listen for signal strength changes. Here is what worked for me. Use this to register the listener:

private TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int currentSignalStrength = 0;
int asu = 0;

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

And this is the actual listener:

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();
        }
    }

Tested on Xperia Z which has the same problems using CellInfoGsm.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top