Android获取所有小区信息

时间:2016-07-18 13:37:35

标签: android cellinfo

我正在研究3G和4G网络监控。我尝试显示4G的RSRP,RSSI和SINR等小区信息(RSCP,RSSI,3G的EcNo)。我发现方法getAllCellInfo()可以为我做的工作,因为它包含我需要的所有东西,但它的值始终为null。 以下是3G小区信息的代码:

 protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

        if (tm.getNetworkType() == 3 || tm.getNetworkType() == 8 || tm.getNetworkType() == 9 || tm.getNetworkType() == 10
        || tm.getNetworkType() == 15) {
            // Network Type is 3G
            setContentView(R.layout.report_val_layout_3g);

            sigLevTextView = (TextView) findViewById(R.id.sigLevTextView);
            rssiTextView = (TextView) findViewById(R.id.rssiTextView);
            sigQualTextView = (TextView) findViewById(R.id.sigQualTextView);
            cellIDTextView = (TextView) findViewById(R.id.cellIDTextView);

            signalStrengthListener = new SignalStrengthListener();

            ((TelephonyManager)getSystemService(TELEPHONY_SERVICE)).listen(signalStrengthListener, SignalStrengthListener.LISTEN_SIGNAL_STRENGTHS);
            tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        }

    try {
                    cellInfoList = tm.getAllCellInfo();
                } catch (Exception e) {
                    Log.d("SignalStrength", "+++++++++++++++++++++++++++++++++++++++++ null array spot 1: " + e);
                }

try {
            cellInfoList = tm.getAllCellInfo();
            for (CellInfo cellInfo : cellInfoList) {
                if (cellInfo instanceof CellInfoWcdma){
                    sigLev = ((CellInfoWcdma) cellInfo).getCellSignalStrength().getDbm();
                    asu = ((CellInfoWcdma) cellInfo).getCellSignalStrength().getAsuLevel();
                    rssi = -113 + 2 * asu;
                    sigQual = 10 * Math.log10(sigLev / rssi); //EcNo
                    cellID = ((CellInfoWcdma) cellInfo).getCellIdentity().getCid();

                }
            }
        } catch  (Exception e) {
            Log.d("SignalStrength", "+++++++++++++++++++++++++++++++ null array spot 2: " + e);
        }

 @Override
    protected void onPause() {
        super.onPause();

        try{
            if(signalStrengthListener != null){tm.listen(signalStrengthListener, SignalStrengthListener.LISTEN_NONE);}
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        try{
            if(signalStrengthListener != null){tm.listen(signalStrengthListener, SignalStrengthListener.LISTEN_NONE);}
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public class SignalStrengthListener extends PhoneStateListener{

        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {

            ((TelephonyManager)getSystemService(TELEPHONY_SERVICE)).listen(signalStrengthListener, SignalStrengthListener.LISTEN_SIGNAL_STRENGTHS);
            tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);


            try {
                cellInfoList = tm.getAllCellInfo();
               for (CellInfo cellInfo : cellInfoList) {
                   if (cellInfo instanceof CellInfoWcdma) {
                       sigLev = ((CellInfoWcdma)cellInfo).getCellSignalStrength().getDbm();
                       asu = ((CellInfoWcdma)cellInfo).getCellSignalStrength().getAsuLevel();
                       rssi = -113 + 2 * asu;
                       sigQual = 10 * Math.log10(sigLev / rssi);
                       cellID = ((CellInfoWcdma)cellInfo).getCellIdentity().getCid();
                   }
               }

        }catch  (Exception e) {
            Log.d("SignalStrength", "+++++++++++++++++++++++++++++++ null array spot 3: " + e);
        }

            sigLevTextView.setText(String.valueOf(sigLev));
            rssiTextView.setText(String.valueOf(rssi));
            sigQualTextView.setText(String.valueOf(sigQual));
            cellIDTextView.setText(String.valueOf(cellID));

            super.onSignalStrengthsChanged(signalStrength);
        }
    }

在清单文件中,我使用了3个权限:ACCESS_COARSE_LOCATION,ACCESS_NETWORK_STATE和READ_PHONE_STATE。 似乎代码在异常n°3中停止,这是日志:

   23316-23316/com.test.a3g.a3gtest E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources
07-18 16:16:06.865 23316-23316/com.test.a3g.a3gtest W/dalvikvm: VFY: unable to resolve check-cast 1798 (Landroid/util/ArrayMap;) in Lcom/android/tools/fd/runtime/MonkeyPatcher;
07-18 16:16:06.865 23316-23316/com.test.a3g.a3gtest D/dalvikvm: VFY: replacing opcode 0x1f at 0x0258
07-18 16:16:06.865 23316-23316/com.test.a3g.a3gtest E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.pruneResourceCache
07-18 16:16:06.865 23316-23316/com.test.a3g.a3gtest W/dalvikvm: VFY: unable to resolve const-class 1798 (Landroid/util/ArrayMap;) in Lcom/android/tools/fd/runtime/MonkeyPatcher;
07-18 16:16:06.865 23316-23316/com.test.a3g.a3gtest D/dalvikvm: VFY: replacing opcode 0x1c at 0x0058
07-18 16:16:06.885 23316-23316/com.test.a3g.a3gtest I/dalvikvm: Could not find method android.content.Context.getSystemService, referenced from method com.test.a3g.a3gtest.MainActivity.access$super
07-18 16:16:06.885 23316-23316/com.test.a3g.a3gtest W/dalvikvm: VFY: unable to resolve virtual method 442: Landroid/content/Context;.getSystemService (Ljava/lang/Class;)Ljava/lang/Object;
07-18 16:16:06.885 23316-23316/com.test.a3g.a3gtest D/dalvikvm: VFY: replacing opcode 0x6f at 0x004b
07-18 16:16:06.885 23316-23316/com.test.a3g.a3gtest I/dalvikvm: Could not find method android.app.Activity.stopLockTask, referenced from method com.test.a3g.a3gtest.MainActivity.access$super
07-18 16:16:06.885 23316-23316/com.test.a3g.a3gtest W/dalvikvm: VFY: unable to resolve virtual method 231: Landroid/app/Activity;.stopLockTask ()V
07-18 16:16:06.885 23316-23316/com.test.a3g.a3gtest D/dalvikvm: VFY: replacing opcode 0x6f at 0x00b9
07-18 16:16:06.890 23316-23316/com.test.a3g.a3gtest E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.test.a3g.a3gtest.MainActivity.access$super
07-18 16:16:06.890 23316-23316/com.test.a3g.a3gtest W/dalvikvm: VFY: unable to resolve check-cast 226 (Landroid/os/PersistableBundle;) in Lcom/test/a3g/a3gtest/MainActivity;
07-18 16:16:06.890 23316-23316/com.test.a3g.a3gtest D/dalvikvm: VFY: replacing opcode 0x1f at 0x00f1
07-18 16:16:06.890 23316-23316/com.test.a3g.a3gtest I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method com.test.a3g.a3gtest.MainActivity.access$super
07-18 16:16:06.890 23316-23316/com.test.a3g.a3gtest W/dalvikvm: VFY: unable to resolve virtual method 424: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList;
07-18 16:16:06.890 23316-23316/com.test.a3g.a3gtest D/dalvikvm: VFY: replacing opcode 0x6f at 0x0101
07-18 16:16:06.890 23316-23316/com.test.a3g.a3gtest I/dalvikvm: Could not find method android.app.Activity.onVisibleBehindCanceled, referenced from method com.test.a3g.a3gtest.MainActivity.access$super
07-18 16:16:06.890 23316-23316/com.test.a3g.a3gtest W/dalvikvm: VFY: unable to resolve virtual method 154: Landroid/app/Activity;.onVisibleBehindCanceled ()V
07-18 16:16:06.890 23316-23316/com.test.a3g.a3gtest D/dalvikvm: VFY: replacing opcode 0x6f at 0x0111
07-18 16:16:06.890 23316-23316/com.test.a3g.a3gtest I/dalvikvm: Could not find method android.app.Activity.onWindowStartingActionMode, referenced from method com.test.a3g.a3gtest.MainActivity.access$super
07-18 16:16:06.890 23316-23316/com.test.a3g.a3gtest W/dalvikvm: VFY: unable to resolve virtual method 158: Landroid/app/Activity;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;

0 个答案:

没有答案