没有人知道如何获得的信号强度,而不必调用onSignalStrengthChanged。与onSignalStrengthchanged的问题是,它被称为当信号强度的变化,我需要根据不同的标准来获得signalstrength的价值。
提前致谢。
在API级别只有17,这里的一些代码,可以在Activity
(或任何其他Context
子类)中使用:
import android.telephony.CellInfo;
import android.telephony.CellInfoCdma;
import android.telephony.CellInfoGsm;
import android.telephony.CellInfoLte;
import android.telephony.CellSignalStrengthCdma;
import android.telephony.CellSignalStrengthGsm;
import android.telephony.CellSignalStrengthLte;
import android.telephony.TelephonyManager;
try {
final TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
for (final CellInfo info : tm.getAllCellInfo()) {
if (info instanceof CellInfoGsm) {
final CellSignalStrengthGsm gsm = ((CellInfoGsm) info).getCellSignalStrength();
// do what you need
} else if (info instanceof CellInfoCdma) {
final CellSignalStrengthCdma cdma = ((CellInfoCdma) info).getCellSignalStrength();
// do what you need
} else if (info instanceof CellInfoLte) {
final CellSignalStrengthLte lte = ((CellInfoLte) info).getCellSignalStrength();
// do what you need
} else {
throw new Exception("Unknown type of cell signal!");
}
}
} catch (Exception e) {
Log.e(TAG, "Unable to obtain cell signal information", e);
}
旧版Android设备需要向听众,没有其他替代(见this link)。
另外,还要确保你的应用程序中包含相应的权限。
您可以通过反射调用访问SignalStrength。请通过链接实现http://blog.ajhodges.com/2013/03/reading-lte-signal-strength-rssi-in.html
有一个在Android的另一个API称为CellInfo。但我不知道通过OnSignalStrengthsChanged()和CellInfo返回的信号强度是否相同或不。
https://developer.android.com/reference/android/telephony/CellSignalStrength.html
基于以上安德烈的回答,在科特林您可以使用此一班轮(API再次17+):
fun getRadioSignalLevel(): Int {
return when (val info = telephonyManager.allCellInfo?.firstOrNull()) {
is CellInfoLte -> info.cellSignalStrength.level
is CellInfoGsm -> info.cellSignalStrength.level
is CellInfoCdma -> info.cellSignalStrength.level
is CellInfoWcdma -> info.cellSignalStrength.level
else -> 0
}
}