gpt4 book ai didi

android - 在不使用 PhoneStateListener onSignalStrengthchanged 的​​情况下获取 SignalStrength

转载 作者:太空狗 更新时间:2023-10-29 15:36:29 25 4
gpt4 key购买 nike

有谁知道如何在不调用 onSignalStrengthChanged 的​​情况下获取信号强度。 onSignalStrengthchanged 的​​问题是它在信号强度发生变化时调用,我需要根据不同的标准获取信号强度的值。

提前致谢。

最佳答案

在 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)。

还要确保您的应用程序包含适当的权限。

关于android - 在不使用 PhoneStateListener onSignalStrengthchanged 的​​情况下获取 SignalStrength,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17618167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com