android 获取wifi和移动网络信号强度

1. 获取wifi信号强度

可以直接监听广播

MyReceiver receiver = new RssiReceiver();

IntentFilter intentFilter = new IntentFilter(WifiManager.RSSI_CHANGED_ACTION);

registerReceiver(receiver,intentFilter);

class MyReceiver extends BroadcastReceiver {

    @Override

    public void onReceive(Context context, Intent intent) {

        int rssi = intent.getIntExtra(WifiManager.EXTRA_NEW_RSSI,-1);

//这函数可以计算出信号的等级

int strength = WifiManager.calculateSignalLevel(rssi, 5);

    }

}

也可以从NetworkCallback中获取

@RequiresApi(api = Build.VERSION_CODES.N)

private void registerNetwork(){

    ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        connMgr.registerDefaultNetworkCallback(new ConnectivityManager.NetworkCallback(){

            @Override

            public void onAvailable(@NonNull Network network) {

                super.onAvailable(network);

                Log.d("NetworkCallback", "onAvailable: "+network);

            }

            @Override

            public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties linkProperties) {

                super.onLinkPropertiesChanged(network, linkProperties);

                Log.d("NetworkCallback", "onLinkPropertiesChanged: "+network);

            }

            @Override

            public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {

                super.onCapabilitiesChanged(network, networkCapabilities);

                Log.d("NetworkCallback", "onCapabilitiesChanged: "+networkCapabilities.toString());

                if(networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)){

//判断是wifi还是移动网络

                    if(networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)){

                        int strength = WifiManager.calculateSignalLevel(networkCapabilities.getSignalStrength(), 5);

                    }else {


                    }

                }

            }

            @Override

            public void onLosing(@NonNull Network network, int maxMsToLive) {

                super.onLosing(network, maxMsToLive);

                Log.d("NetworkCallback", "onLosing: "+network);

            }

            @Override

            public void onLost(@NonNull Network network) {

                super.onLost(network);

                Log.d("NetworkCallback", "onLost: "+network);

            }

            @Override

            public void onUnavailable() {

                super.onUnavailable();

                Log.d("NetworkCallback", "onUnavailable: ");

            }

        });

    }

}

注意 networkCapabilities.getSignalStrength()在API29的时候才可以调用,低于29的时候为隐藏方法.可用反射调用。onCapabilitiesChanged这个函数在wifi强度变化时会调用,但是移动网络强度变化时确不会调用,而且在移动网络时getSignalStrength方法获取到的也不是信号强度。

2. 获取移动网络强度

可以直接监听PhoneStateListener,这里只是单卡的情况

private void phoneStateListener(){

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

    mTelephonyManager.listen(new PhoneStateListener(){

        @RequiresApi(api = Build.VERSION_CODES.M)

        @Override

        public void onSignalStrengthsChanged(SignalStrength signalStrength) {

            super.onSignalStrengthsChanged(signalStrength);

            int level = signalStrength.getLevel();

            Log.d("phoneStateListener", "onSignalStrengthsChanged: level "+level);

        }

        @Override

        public void onServiceStateChanged(ServiceState state) {

            Log.d("phoneStateListener", "onServiceStateChanged: "+state.toString());

        }

        @Override

        public void onDataConnectionStateChanged(int state, int networkType) {

            Log.d("phoneStateListener", "onDataConnectionStateChanged: state "+state+" networkType "+networkType);

        }

    }, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE | PhoneStateListener.LISTEN_SERVICE_STATE);

}


如果是双卡的情况可以通过反射调用。但是我发现一个非常奇怪的现象,当打开相机时这里面的所有方法都会停止调用,原因不知道。没办法,只能取修改系统源码了,修改SystemUI的MobileSignalController.java

@Override

public void onSignalStrengthsChanged(SignalStrength signalStrength) {

    if (DEBUG) {

        Log.d(mTag, "onSignalStrengthsChanged signalStrength=" + signalStrength +

                ((signalStrength == null) ? "" : (" level=" + signalStrength.getLevel())));

    }

    mSignalStrength = signalStrength;

    updateTelephony();

//信号有强度有变化时发送广播

Intent intent = new Intent("android.net.mobile.RSSI_CHANGED");

intent.putExtra("EXTRA_NEW_RSSI", signalStrength.getLevel());

mContext.sendBroadcast(intent);

}

你可能感兴趣的:(android 获取wifi和移动网络信号强度)