android5.1.1 获取当前移动信号强度
来源:互联网 发布:电子科技大学软件学院 编辑:程序博客网 时间:2024/05/01 20:19
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; } }}
//少了什么可以评论留意,我看到了就找找
0 0
- android5.1.1 获取当前移动信号强度
- Andoid 获取信号强度
- Android获取信号强度
- WIFI信号强度获取
- Android 获取信号强度
- 获取信号强度
- Android 信号强度的获取
- Android 信号强度的获取
- Android中获取信号强度
- 解决获取3G信号强度
- android获取wifi信号强度
- Android获取WIFI信号强度
- android获取wifi信号强度
- Android 获取wifi信号强度
- iOS 获取wifi信号强度
- iOS 获取蜂窝信号强度
- android 获取wifi 信号质量,信号强度
- Android获取当前已连接的wifi信号强度的方法
- LeetCode 237 Delete Node in a Linked List(技巧)
- React Native布局之FlexBox
- php学习路线图
- 多通道接收机幅相校准测试系统的设计
- 1
- android5.1.1 获取当前移动信号强度
- strcat()的实现
- GitHub上最火的Android开源项目,所有开源项目都有详细资料和配套视频
- Java的IO与NIO简单介绍
- Delphi 的泛型和匿名函数:对 TObjectList<> 做排序
- Spinner中getResource()空指针问题以及Context传值问题
- Oracle学习笔记
- 通过鼠标控制镜头绕着某物体旋转
- java代码设置selector中的颜色值失效