在Android中我们可以通过下面这段代码获取SIM的iccid
TelephonyManager telephonyManager = (TelephonyManager)
getSystemService(TELEPHONY_SERVICE);
String simSerialNumber = telephonyManager.getSimSerialNumber();
很好,重点来了,如果我们此时需要获取带字母的ICCID,就会发现获取的并不全,在带字母的前一位就停止获取了,我就不去阐述原因了,百度一大把,今天我们介绍怎么正确获取所有的20位卡号:
1.第一步:连接你所使用的机器,打开电脑adb,输入命令adb shell getprop,就会获取机器的一连串信息:
此时我们只关注iccid,此时我的机器的iccid的标志位是: ril.yuga.iccid(不同的机器可能有不同的标志位,注意仔细查看)
2.第二步,在代码中使用标志位ril.yuga.iccid获取到物联网卡的iccid
看到这,应该有人能看懂我的操作,没错,我使用的是su命令,我再贴一下代码:
CommandUtil
package com.example.test.utils
import android.util.Log
import java.io.BufferedReader
import java.io.DataOutputStream
import java.io.IOException
import java.io.InputStreamReader
class CommandUtil {
companion object {
var TAG = "CommandUtil"
fun execRootCmdSilent(cmd: String?, onEnd: ((str: String) -> Unit)? = null) {
var process: Process? = null
var os: DataOutputStream? = null
var successResult: BufferedReader? = null
var errorResult: BufferedReader? = null
var successMsg: StringBuilder? = null
var errorMsg: StringBuilder? = null
val commandResult =
ShellUtils.CommandResult(1)
try {
process = Runtime.getRuntime().exec("su")
os = DataOutputStream(process.outputStream)
os.writeBytes(cmd)
os.writeBytes("exitn")
os.flush()
os.close()
commandResult.result = process.waitFor()
successMsg = StringBuilder()
errorMsg = StringBuilder()
successResult =
BufferedReader(InputStreamReader(process.inputStream))
errorResult =
BufferedReader(InputStreamReader(process.errorStream))
var s: String?
while (successResult.readLine().also { s = it } != null) successMsg.append(s)
while (errorResult.readLine().also { s = it } != null) errorMsg.append(s)
commandResult.successMsg = successMsg.toString()
commandResult.errorMsg = errorMsg.toString()
Log.e(
TAG,
commandResult.result.toString() + " | " + commandResult.successMsg + " | " + commandResult.errorMsg
)
if (commandResult.result == 0) {
//获取成功
onEnd!!.invoke(commandResult.successMsg)
} else {
onEnd!!.invoke("iccid卡获取异常")
}
} catch (e: Exception) {
val errmsg = e.message
if (errmsg != null) {
Log.i(TAG, errmsg)
onEnd!!.invoke("errmsg$errmsg")
}
} finally {
try {
os?.close()
successResult?.close()
errorResult?.close()
} catch (e: IOException) {
val errmsg = e.message
if (errmsg != null) {
Log.v(TAG, errmsg)
} else {
e.printStackTrace()
}
}
process?.destroy()
}
}
}
}
ShellUtilspackage com.example.test.utils; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; public class ShellUtils { public static final String COMMAND_SU = "su"; public static final String COMMAND_SH = "sh"; public static final String COMMAND_EXIT = "exitn"; public static final String COMMAND_LINE_END = "n"; private ShellUtils() { throw new AssertionError(); } public static boolean checkRootPermission() { return execCommand("echo root", true, false).result == 0; } public static CommandResult execCommand(String command, boolean isRoot) { return execCommand(new String[] { command }, isRoot, true); } public static CommandResult execCommand(Listcommands, boolean isRoot) { return execCommand( commands == null ? null : commands.toArray(new String[] {}), isRoot, true); } public static CommandResult execCommand(String[] commands, boolean isRoot) { return execCommand(commands, isRoot, true); } public static CommandResult execCommand(String command, boolean isRoot, boolean isNeedResultMsg) { return execCommand(new String[] { command }, isRoot, isNeedResultMsg); } public static CommandResult execCommand(List commands, boolean isRoot, boolean isNeedResultMsg) { return execCommand( commands == null ? null : commands.toArray(new String[] {}), isRoot, isNeedResultMsg); } public static CommandResult execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) { int result = -1; if (commands == null || commands.length == 0) { return new CommandResult(result, null, null); } Process process = null; BufferedReader successResult = null; BufferedReader errorResult = null; StringBuilder successMsg = null; StringBuilder errorMsg = null; DataOutputStream os = null; try { process = Runtime.getRuntime().exec( isRoot ? COMMAND_SU : COMMAND_SH); os = new DataOutputStream(process.getOutputStream()); for (String command : commands) { if (command == null) { continue; } // donnot use os.writeBytes(commmand), avoid chinese charset // error os.write(command.getBytes()); os.writeBytes(COMMAND_LINE_END); os.flush(); } os.writeBytes(COMMAND_EXIT); os.flush(); result = process.waitFor(); // get command result if (isNeedResultMsg) { successMsg = new StringBuilder(); errorMsg = new StringBuilder(); successResult = new BufferedReader(new InputStreamReader( process.getInputStream())); errorResult = new BufferedReader(new InputStreamReader( process.getErrorStream())); String s; while ((s = successResult.readLine()) != null) { successMsg.append(s); } while ((s = errorResult.readLine()) != null) { errorMsg.append(s); } } } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (os != null) { os.close(); } if (successResult != null) { successResult.close(); } if (errorResult != null) { errorResult.close(); } } catch (IOException e) { e.printStackTrace(); } if (process != null) { process.destroy(); } } return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null : errorMsg.toString()); } public static class CommandResult { public int result; public String successMsg; public String errorMsg; public CommandResult(int result) { this.result = result; } public CommandResult(int result, String successMsg, String errorMsg) { this.result = result; this.successMsg = successMsg; this.errorMsg = errorMsg; } } } 这就是全部的操作了,想要正确获取到,主要看第一步获取到的iccid标志位



