美文网首页
Android实时监控4G手机网络信号强度

Android实时监控4G手机网络信号强度

作者: 汶水一方 | 来源:发表于2017-12-29 11:25 被阅读354次
    
        public  void getPhoneState(Context context) {
            final TelephonyManager telephonyManager = (TelephonyManager)     context.getSystemService(Context.TELEPHONY_SERVICE);
            PhoneStateListener MyPhoneListener = new PhoneStateListener() {
                @Override
                //获取对应网络的ID,这个方法在这个程序中没什么用处
                public void onCellLocationChanged(CellLocation location) {
                    if (location instanceof GsmCellLocation) {
                        int CID = ((GsmCellLocation) location).getCid();
                    } else if (location instanceof CdmaCellLocation) {
                        int ID = ((CdmaCellLocation) location).getBaseStationId();
                    }
                }
                //系统自带的服务监听器,实时监听网络状态
                @Override
                public void onServiceStateChanged(ServiceState serviceState) {
                    super.onServiceStateChanged(serviceState);
                }
                //这个是我们的主角,就是获取对应网络信号强度
                @Override
                public void onSignalStrengthsChanged(SignalStrength signalStrength) {
                    //这个ltedbm 是4G信号的值
                    String signalinfo = signalStrength.toString();
                    String[] parts = signalinfo.split(" ");
                    int ltedbm = Integer.parseInt(parts[9]);
                    //这个dbm 是2G和3G信号的值
                    int asu = signalStrength.getGsmSignalStrength();
                    int dbm = -113 + 2 * asu;
    
                    if (telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_LTE) {
                        Log.d("NetWorkUtil", "网络:LTE 信号强度:" + ltedbm + "======Detail:" + signalinfo);
                    } else if (telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSDPA ||
                            telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSPA ||
                            telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSUPA ||
                            telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS)                                               {
                        String bin;
                        if (dbm > -75) {
                            bin = "网络很好";
                        } else if (dbm > -85) {
                            bin = "网络不错";
                        } else if (dbm > -95) {
                            bin = "网络还行";
                        } else if (dbm > -100) {
                            bin = "网络很差";
                        } else {
                            bin = "网络错误";
                        }
                        Log.d("NetWorkUtil", "网络:WCDMA 信号值:" + dbm + "========强度:" + bin + "======Detail:" + signalinfo);
                    } else {
                        String bin;
                        if (asu < 0 || asu >= 99) bin = "网络错误";
                        else if (asu >= 16) bin = "网络很好";
                        else if (asu >= 8) bin = "网络不错";
                        else if (asu >= 4) bin = "网络还行";
                        else bin = "网络很差";
                        Log.d("NetWorkUtil", "网络:GSM 信号值:" + dbm + "========强度:" + bin + "======Detail:" + signalinfo);
                    }
                    super.onSignalStrengthsChanged(signalStrength);
                }
            };
            telephonyManager.listen(MyPhoneListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        }
    
    

    相关文章

      网友评论

          本文标题:Android实时监控4G手机网络信号强度

          本文链接:https://www.haomeiwen.com/subject/ynrigxtx.html