我在安卓系统上遇到了一个问题。我想知道这个设备是否连接到GSM网络,这样它就可以发送短信了。
我试过两种方法,结果都是一样的。在Moto G 4G上,我可以确定它是否有接入权限。在Redmi 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 ==>值99未知
在Moto号上:
SignalStrength: 99 0 -120 -160 -120 -1 -1 18 -109 -8 116 2147483647 2147483647 0到31之间的==>值是可以的。
以前有人面对过这个问题并解决了吗?我已经读到,这个问题也存在于一些三星银河。
谢谢!
发布于 2017-09-24 16:11:01
起作用了!
这是我的最后一部作品,行得通。如果手机有很大的正常运行时间值,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;
}
}https://stackoverflow.com/questions/43955280
复制相似问题