gpt4 book ai didi

android - PhoneStateListener Android 错误值

转载 作者:行者123 更新时间:2023-11-30 00:32:57 26 4
gpt4 key购买 nike

我在 android 上遇到问题。我想知道设备是否已连接到 GSM 网络,以便它可以发送短信。

两种方式我都试过了,结果都是一样的。在 Moto G 4G 上,我能够确定它是否可以访问。在 Redmi Note 4 上它总是返回 false。

第一个:

public static boolean hasCellularAccess(Context context) {
TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

if(telephonyManager == null || telephonyManager.getAllCellInfo() == null || telephonyManager.getAllCellInfo().size() == 0){
return false;
}

if (telephonyManager.getAllCellInfo().get(0) instanceof CellInfoLte){
CellInfoLte cellInfoLte = (CellInfoLte)telephonyManager.getAllCellInfo().get(0);
CellSignalStrength cellSignalStrengthGsm = cellInfoLte.getCellSignalStrength();
return cellSignalStrengthGsm.getLevel() > 0;
}

CellInfoGsm cellinfogsm = (CellInfoGsm) telephonyManager.getAllCellInfo().get(0);
CellSignalStrength cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
return cellSignalStrengthGsm.getLevel() > 0;
}

第二个:

public class CustomPhoneListener extends PhoneStateListener {
private static final String TAG = "PhoneListener";

@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
Log.i(TAG, "onSignalStrengthsChanged: " + signalStrength);
if (signalStrength.isGsm()) {
Log.i(TAG, "onSignalStrengthsChanged: getGsmSignalStrength "
+ signalStrength.getGsmSignalStrength() + signalStrength);
}
try {

Method[] methods = android.telephony.SignalStrength.class
.getMethods();
for (Method mthd : methods) {
if (mthd.getName().equals("getLteSignalStrength")) {
Log.i(TAG,
"onSignalStrengthsChanged: " + mthd.getName() + " "
+ mthd.invoke(signalStrength));
}
}
} catch (SecurityException | IllegalArgumentException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}

}}

在我的红米手机上:

SignalStrength: 0 48 -120 -160 -7 0 -1 99 -93 -6 282 2147483647 gsm|lte 1 1 1

==> value 99 is unknown

在摩托上:

SignalStrength: 99 0 -120 -160 -120 -1 -1 18 -109 -8 116 2147483647 2147483647 gsm|lte 0 -108 -1 false 0 0 0 0 2 99 99 99 4

==> value between 0 and 31 is ok

有人遇到过这个问题并解决了吗?我读到一些三星 Galaxy 也存在这个问题。

谢谢!

最佳答案

有效!

这是我的最终作品,它有效。如果手机的正常运行时间值很大,API 似乎会返回错误的值。

public static boolean hasCellularAccess(Context context) {
TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(telephonyManager == null || telephonyManager.getAllCellInfo() == null || telephonyManager.getAllCellInfo().size() == 0){
return false;
}

if (telephonyManager.getAllCellInfo().get(0) instanceof CellInfoLte){
CellInfoLte cellInfoLte = (CellInfoLte)telephonyManager.getAllCellInfo().get(0);
CellSignalStrength cellSignalStrengthGsm = cellInfoLte.getCellSignalStrength();
return cellSignalStrengthGsm.getLevel() > 0;
}

if(telephonyManager.getAllCellInfo().get(0) instanceof CellInfoGsm){
CellInfoGsm cellinfogsm = (CellInfoGsm) telephonyManager.getAllCellInfo().get(0);
CellSignalStrength cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
return cellSignalStrengthGsm.getLevel() > 0;
}else {
CellInfoWcdma cellinfogsm = (CellInfoWcdma) telephonyManager.getAllCellInfo().get(0);
CellSignalStrength cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
return cellSignalStrengthGsm.getLevel() > 0;
}
}

关于android - PhoneStateListener Android 错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43955280/

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