android5.1.1 获取当前移动信号强度

    ConnectivityManager mConnectivityManager;
    TelephonyManager mTelephonyManager;
    private final BitSet mValidatedTransports = new BitSet();
    // The current user ID.
    private int mCurrentUserId;
    private boolean mInetCondition; // Used for Logging and demo.
    private ServiceState mServiceState;


    private ImageView mIvSignal;//显示信号的ImageView
    static final int TELEPHONY_NO_NETWORK = R.drawable.stat_sys_signal_null;
    static final int TELEPHONY_NO_SIM = R.drawable.stat_sys_no_sims;

    static final int[][] SIGNAL_STRENGTH = {
            { R.drawable.stat_sys_signal_0,
                    R.drawable.stat_sys_signal_1,
                    R.drawable.stat_sys_signal_2,
                    R.drawable.stat_sys_signal_3,
                    R.drawable.stat_sys_signal_4 },
            { R.drawable.stat_sys_signal_0_fully,
                    R.drawable.stat_sys_signal_1_fully,
                    R.drawable.stat_sys_signal_2_fully,
                    R.drawable.stat_sys_signal_3_fully,
                    R.drawable.stat_sys_signal_4_fully }
    };



@Override protected void onCreate(final Bundle savedInstanceState) {
        setContentView(R.layout.activity_main_lh2);
        mIvSignal = (ImageView) findViewById(R.id.iv_signal);

        mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        mTelephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        mTelephonyManager.listen(new PhoneStateListener(){
            @Override
            public void onSignalStrengthsChanged(SignalStrength signalStrength) {
                super.onSignalStrengthsChanged(signalStrength);
                updateConnectivity();
                if (hasSimCard()) {
                    if (hasService() && signalStrength != null) {
                        mIvSignal.setImageResource(SIGNAL_STRENGTH[mInetCondition ? 1 : 0][signalStrength.getLevel()]);
                    } else {
                        mIvSignal.setImageResource(TELEPHONY_NO_NETWORK);
                    }
                } else {
                    mIvSignal.setImageResource(TELEPHONY_NO_SIM);
                }
            }
            @Override
            public void onServiceStateChanged(ServiceState state) {
                mServiceState = state;
            }
            @Override
            public void onDataConnectionStateChanged(int state, int networkType) {
            }
        },
                PhoneStateListener.LISTEN_SIGNAL_STRENGTHS //监测信号变化
                        |PhoneStateListener.LISTEN_SERVICE_STATE //监测是否有服务
                        | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE //监测连接网络类型变化
        );

    private void updateConnectivity() {
        if (mInetCondition) {
            return;
        }
        mValidatedTransports.clear();
        for (NetworkCapabilities nc :
                mConnectivityManager.getDefaultNetworkCapabilitiesForUser(mCurrentUserId)) {
            for (int transportType : nc.getTransportTypes()) {
                if (nc.hasCapability(NET_CAPABILITY_VALIDATED)) {
                    mValidatedTransports.set(transportType);
                }
            }
        }
        mInetCondition = !mValidatedTransports.isEmpty();
    }

    /**
     * 判断是否包含SIM卡
     *
     * @return 状态
     */
    public boolean hasSimCard() {
        int simState = mTelephonyManager.getSimState();
        boolean result = true;
        switch (simState) {
            case TelephonyManager.SIM_STATE_ABSENT:
                result = false; // 没有SIM卡
                break;
            case TelephonyManager.SIM_STATE_UNKNOWN:
                result = false;
                break;
        }
        return result;
    }

    private boolean hasService() {
        if (mServiceState != null) {
            // Consider the device to be in service if either voice or data
            // service is available. Some SIM cards are marketed as data-only
            // and do not support voice service, and on these SIM cards, we
            // want to show signal bars for data service as well as the "no
            // service" or "emergency calls only" text that indicates that voice
            // is not available.
            switch (mServiceState.getVoiceRegState()) {
                case ServiceState.STATE_POWER_OFF:
                    return false;
                case ServiceState.STATE_OUT_OF_SERVICE:
                case ServiceState.STATE_EMERGENCY_ONLY:
                    return mServiceState.getDataRegState() == ServiceState.STATE_IN_SERVICE;
                default:
                    return true;
            }
        } else {
            return false;
        }
    }



}
//少了什么可以评论留意,我看到了就找找

你可能感兴趣的:(android5.1.1 获取当前移动信号强度)