Android獲取移動網絡信號強度的方法
判斷有沒有 SIM 卡
獲取移動網絡的信號,一定要判斷是否插瞭 SIM 卡,沒插卡肯定是沒法獲取的
/** * 判斷是否包含SIM卡 * * @return 狀態 */ public static boolean hasSimCard(Context context) { TelephonyManager telMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); int simState = telMgr.getSimState(); boolean result = true; switch (simState) { case TelephonyManager.SIM_STATE_ABSENT: case TelephonyManager.SIM_STATE_UNKNOWN: result = false; // 沒有SIM卡 break; } return result; }
獲取信號強度
如果插瞭 SIM 卡,就可以通過下邊方法來獲取移動網絡的信號強度(單位 dBM),使用的是 TelephonyManager 來監聽
private void getMobileNetworkSignal() { if (!PhoneUtils.hasSimCard(mcontext)) { logger.info("getMobileNetworkSignal: no sim card"); return; } TelephonyManager mTelephonyManager = (TelephonyManager) BaseApplication.getAppContext().getSystemService(Context.TELEPHONY_SERVICE); if (mTelephonyManager != null) { mTelephonyManager.listen(new PhoneStateListener() { @Override public void onSignalStrengthsChanged(SignalStrength signalStrength) { super.onSignalStrengthsChanged(signalStrength); int asu = signalStrength.getGsmSignalStrength(); int lastSignal = -113 + 2 * asu; if (lastSignal > 0) { mobileNetworkSignal = lastSignal + "dBm"; } logger.info("Current mobileNetworkSignal:" + lastSignal + " dBm"); } }, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS); } }
也可以添加網絡類型的判斷,來獲取是何種網絡的信號強度
int netWorkType = getNetWorkType(mContext); switch (netWorkType) { case NETWORKTYPE_WIFI: mTextView.setText("當前網絡為wifi,信號強度為:" + gsmSignalStrength); break; case NETWORKTYPE_2G: mTextView.setText("當前網絡為2G移動網絡,信號強度為:" + gsmSignalStrength); break; case NETWORKTYPE_4G: mTextView.setText("當前網絡為4G移動網絡,信號強度為:" + gsmSignalStrength); break; case NETWORKTYPE_NONE: mTextView.setText("當前沒有網絡,信號強度為:" + gsmSignalStrength); break; case -1: mTextView.setText("當前網絡錯誤,信號強度為:" + gsmSignalStrength); break; }
用到的獲取網絡類型的方法:
public static int getNetWorkType(Context context) { int mNetWorkType = -1; ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = manager.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { String type = networkInfo.getTypeName(); if (type.equalsIgnoreCase("WIFI")) { mNetWorkType = NETWORKTYPE_WIFI; } else if (type.equalsIgnoreCase("MOBILE")) { return isFastMobileNetwork(context) ? NETWORKTYPE_4G : NETWORKTYPE_2G; } } else { mNetWorkType = NETWORKTYPE_NONE;//沒有網絡 } return mNetWorkType; } /** * 判斷網絡類型 */ private static boolean isFastMobileNetwork(Context context) { TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_LTE) { return true; } return false; }
最後添加必要權限即可
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
以上就是Android獲取移動網絡信號強度的方法的詳細內容,更多關於Android獲取網絡信號強度的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- None Found