Skip to content

Instantly share code, notes, and snippets.

@yihongyuelan
Last active September 25, 2018 03:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yihongyuelan/83d5d850573ffa28390f07e74f4d0673 to your computer and use it in GitHub Desktop.
Save yihongyuelan/83d5d850573ffa28390f07e74f4d0673 to your computer and use it in GitHub Desktop.
Check the validity of the SIM card for Android
public abstract class CellSignalStrength {
public static final int SIGNAL_STRENGTH_NONE_OR_UNKNOWN = 0;
public static final int SIGNAL_STRENGTH_POOR = 1;
public static final int SIGNAL_STRENGTH_MODERATE = 2;
public static final int SIGNAL_STRENGTH_GOOD = 3;
public static final int SIGNAL_STRENGTH_GREAT = 4;
... ...
}
//If the signal strength of SIM card is efficacious, the level must greater than 0.
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static boolean isNetworkReady(Context context) {
TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (manager != null) {
// Need Manifest.permission.ACCESS_COARSE_LOCATION add do check by your own
@SuppressLint("MissingPermission") List<CellInfo> infoList = manager.getAllCellInfo();
for (CellInfo info : infoList) {
if (info.isRegistered()) {
if (info instanceof CellInfoGsm) {
CellSignalStrengthGsm gsm = ((CellInfoGsm) info).getCellSignalStrength();
if (gsm.getLevel() > 0) {
return true;
}
} else if (info instanceof CellInfoCdma) {
CellSignalStrengthCdma cdma = ((CellInfoCdma) info).getCellSignalStrength();
if (cdma.getLevel() > 0) {
return true;
}
} else if (info instanceof CellInfoWcdma) {
CellSignalStrengthWcdma wcdma = ((CellInfoWcdma) info).getCellSignalStrength();
if (wcdma.getLevel() > 0) {
return true;
}
} else if (info instanceof CellInfoLte) {
CellSignalStrengthLte lte = ((CellInfoLte) info).getCellSignalStrength();
if (lte.getLevel() > 0) {
return true;
}
}
}
}
}
return false;
}
@yihongyuelan
Copy link
Author

检查SIM卡有效性,需要 权限。原理即检测SIM卡信号,如果信号大于0即表示SIM卡有效。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment