本文介绍了TelephonyManager.getAllCellInfo()返回Null或不显示任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正遇到 getAllCellInfo()的问题.

应用具有所需的权限:

这是我的代码:

1- 侦听器

private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
        @TargetApi(Build.VERSION_CODES.M)
        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            super.onSignalStrengthsChanged(signalStrength);

2- SignalStrength中的信息

TextView comparisonText = (TextView) findViewById(R.id.textViewComparison);
            Object ssFieldValueRsrp = null;
            Object ssFieldValueRsrq = null;
            Object ssFieldValueRssnr = null;
            Object ssFieldValueCqi=null;
            try {
                Field privateStringSsFieldRSRQ = SignalStrength.class.getDeclaredField("mLteRsrq");
                Field privateStringSsFieldRSRP = SignalStrength.class.getDeclaredField("mLteRsrp");
                Field privateStringSsFieldRssnr = SignalStrength.class.getDeclaredField("mLteRssnr");
                Field privateStringSsFieldCqi = SignalStrength.class.getDeclaredField("mLteCqi");

                privateStringSsFieldRSRQ.setAccessible(true);
                ssFieldValueRsrq = privateStringSsFieldRSRQ.get(signalStrength);

                privateStringSsFieldRSRP.setAccessible(true);
                ssFieldValueRsrp = privateStringSsFieldRSRP.get(signalStrength);

                privateStringSsFieldRssnr.setAccessible(true);
                ssFieldValueRssnr = privateStringSsFieldRssnr.get(signalStrength);

                privateStringSsFieldCqi.setAccessible(true);
                ssFieldValueCqi = privateStringSsFieldCqi.get(signalStrength);


            } catch (NoSuchFieldException ex) {
            } catch (IllegalAccessException x) {
            }
            String ssRsrp = Integer.toString((int) ssFieldValueRsrp);
            String ssRsrq = Integer.toString((int) ssFieldValueRsrq);
            String ssRssnr = Integer.toString((int) ssFieldValueRssnr);

            String ssCqi = Integer.toString((int) ssFieldValueCqi);

            String headerString = "Info from \"SignalStrength\":";
            SpannableString spannableHeaderString = new SpannableString(headerString);
            spannableHeaderString.setSpan(new UnderlineSpan(), 0, spannableHeaderString.length(), 0);

            comparisonText.setText(spannableHeaderString);
            comparisonText.append
                    (
                            "\nRSRP: " + ssRsrp
                                    + "\nRSRQ: " + ssRsrq
                                    +"\nCQI: "+ ssCqi
                            +"\nRSSNR: "+ ssRssnr
                    );

3-来自CellSignalStrengthLte的信息

Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            List<android.telephony.CellInfo> infor = tm.getAllCellInfo();
            for (android.telephony.CellInfo info : infor)
            {
                if (info instanceof CellInfoLte)
                {
                    CellSignalStrengthLte ss = ((CellInfoLte) info).getCellSignalStrength();
                    //theButton.setText( ss.toString());

                    Object fieldValueRSRP = null;
                    Object fieldValueRSRQ = null;
                    Object fieldValueRssnr = null;
                    Object fieldValueCqi=null;

                    try
                    {
                        Field privateStringFieldRSRQ = CellSignalStrengthLte.class.getDeclaredField("mRsrq");
                        Field privateStringFieldRSRP = CellSignalStrengthLte.class.getDeclaredField("mRsrp");
                        Field privateStringFieldCqi = CellSignalStrengthLte.class.getDeclaredField("mCqi");
                        Field privateStringFieldRSSNR = CellSignalStrengthLte.class.getDeclaredField("mRssnr");

                        privateStringFieldRSRQ.setAccessible(true);
                        fieldValueRSRQ = privateStringFieldRSRQ.get(ss);

                        privateStringFieldRSRP.setAccessible(true);
                        fieldValueRSRP = privateStringFieldRSRP.get(ss);

                        privateStringFieldRSSNR.setAccessible(true);
                        fieldValueRssnr = privateStringFieldRSSNR.get(ss);

                        privateStringFieldCqi.setAccessible(true);
                        fieldValueCqi = privateStringFieldCqi.get(ss);

                    }
                    catch (NoSuchFieldException ex) {}
                    catch (IllegalAccessException x) {}
                    String rsrp = Integer.toString((int) fieldValueRSRP);
                    String rsrq = Integer.toString((int) fieldValueRSRQ);

                    String rssnr = Integer.toString((int) fieldValueRssnr);
                    String cqi = Integer.toString((int) fieldValueCqi);

                    headerString = "Info from \"CellSignalStrengthLte\":";
                    spannableHeaderString = new SpannableString(headerString);
                    spannableHeaderString.setSpan( new UnderlineSpan(), 0, spannableHeaderString.length(), 0);

                    theText.setText
                    (
                        "\nAltitude: " + loc.getAltitude() + "\n\n"+loc.getLongitude()+"\n\n"+loc.getLatitude()+"\n\n"
                    );
                    theText.append(spannableHeaderString);
                    theText.append
                    (
                        "\nRSRP: " + rsrp
                        + "\nRSRQ: " + rsrq
                                + "\nCQI: " + cqi
                                + "\nRSSNR: " + rssnr
                    );
                }
            }

华为Y6II:棉花糖(android 6):

应用运行平稳,但功能被跳过

华为Nova3i(Android 9)

app在 List< android.telephony.CellInfo>上的空指针崩溃.infor = tm.getAllCellInfo();

app crashes with null pointer on List<android.telephony.CellInfo> infor = tm.getAllCellInfo();

Samsun S10(android 9)

应用运行正常

推荐答案

已解决:

此问题与某些双SIM卡手机有关

This issue is related to some dual SIM phones

这篇关于TelephonyManager.getAllCellInfo()返回Null或不显示任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 14:10