添加以下权限到AndroidManifest.xml中:
确保应用已被正确签名。
检查是否设备支持ICC,并且已安装正确的SIM卡。
检查是否运营商支持ICC,启用并激活了SIM卡。
示例代码:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER) == PackageManager.PERMISSION_GRANTED) {
// Permission is granted
} else {
// Permission is not granted, request for permission
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER}, REQUEST_CODE);
}
// Check if the device has ICC and SIM
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY) {
// SIM card is ready
if (telephonyManager.hasIccCard()) {
// Device has ICC
} else {
// Device does not have ICC
}
} else {
// SIM card not found or not ready
}
// Checking if network is available
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null || !networkInfo.isConnected()) {
// Network not available
} else {
// Network is available
}