我在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();
}
}}
在Redmi上我有:
SignalStrength:0 48 -120 -160 -7 0 -1 99 -93 -6 282 2147483647 gsm | lte 1 1 1
==>值99未知
在Moto上:
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
==> 0到31之间的值是好的
之前有人遇到过这个问题并解决了吗? 我已经读过一些三星Galaxy也存在这个问题。
谢谢!
答案 0 :(得分:1)
有效!
所以这是我最后的工作,它的确有效。如果手机的正常运行时间值很长,那么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;
}
}