Last active
September 25, 2018 03:16
-
-
Save yihongyuelan/83d5d850573ffa28390f07e74f4d0673 to your computer and use it in GitHub Desktop.
Check the validity of the SIM card for Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
检查SIM卡有效性,需要 权限。原理即检测SIM卡信号,如果信号大于0即表示SIM卡有效。