Android 获取手机信号强度

获取手机信号强度:4G(LTE),3G(WCDMA),2G(GSM),代码如下:

 public static 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(" ");
            String ltedbm = String .valueof(parts[9]);
             //这个dbm 是2G和3G信号的值
            int asu = signalStrength.getGsmSignalStrength();
            int dbm = -113 + 2 * asu;

            if (telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_LTE) {
                Log.i("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.i("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.i("NetWorkUtil", "网络:GSM 信号值:" + dbm + "========强度:" + bin + "======Detail:" + signalinfo);
            }
            super.onSignalStrengthsChanged(signalStrength);
        }
    };
    telephonyManager.listen(MyPhoneListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}

调用方式:

getPhoneState(this);
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值