我们大家看到标题就应该明白了,android也是可以获得我们手机的cpu序列号的,这证明了android是一个非常强大的系统,虽然现在还是有一些小的bug,但是到目前为止,android是一个非常好的手机系统,之所以非常好,android才可以和苹果抗衡,形成手机的流行操作系统。不多说了,下面我们就来看看代码吧:
/**
* 获取CPU序列号
*
* @return CPU序列号(16位)
* 读取失败为"0000000000000000"
*/
public static String getCPUSerial() {
String str = "", strCPU = "", cpuAddress = "0000000000000000";
try {
//读取CPU信息
Process pp = Runtime.getRuntime().exec("cat /proc/cpuinfo");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
//查找CPU序列号
for (int i = 1; i < 100; i++) {
str = input.readLine();
if (str != null) {
//查找到序列号所在行
if (str.indexOf("Serial") > -1) {
//提取序列号
strCPU = str.substring(str.indexOf(":") + 1,
str.length());
//去空格
cpuAddress = strCPU.trim();
break;
}
}else{
//文件结尾
break;
}
}
} catch (IOException ex) {
//赋予默认值
ex.printStackTrace();
}
return cpuAddress;
}
从上面的代码来看,我们还是先写一个带有字符串的类,我们在声明几个变量,这几个变量是我们接受序列号的。完事就是用我们经常用的for()循环,这样,系统就是自动的一行一行来查找,我们在做一个判断这个判断就是查找序列号的行数,记住了在if()里要写不等于null。最后就是我们的序列号了,但记住的一点就是,eoe和大家说千万不要忘了要return cpuAddress,这个是非常重要的。没有这个我们就得不到这个值。那我们做的就白费了。虽然代码不是很难,但是我还是拿出来给大家分享一下。
/**
* 获取CPU序列号
*
* @return CPU序列号(16位)
* 读取失败为"0000000000000000"
*/
public static String getCPUSerial() {
String str = "", strCPU = "", cpuAddress = "0000000000000000";
try {
//读取CPU信息
Process pp = Runtime.getRuntime().exec("cat /proc/cpuinfo");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
//查找CPU序列号
for (int i = 1; i < 100; i++) {
str = input.readLine();
if (str != null) {
//查找到序列号所在行
if (str.indexOf("Serial") > -1) {
//提取序列号
strCPU = str.substring(str.indexOf(":") + 1,
str.length());
//去空格
cpuAddress = strCPU.trim();
break;
}
}else{
//文件结尾
break;
}
}
} catch (IOException ex) {
//赋予默认值
ex.printStackTrace();
}
return cpuAddress;
}
从上面的代码来看,我们还是先写一个带有字符串的类,我们在声明几个变量,这几个变量是我们接受序列号的。完事就是用我们经常用的for()循环,这样,系统就是自动的一行一行来查找,我们在做一个判断这个判断就是查找序列号的行数,记住了在if()里要写不等于null。最后就是我们的序列号了,但记住的一点就是,eoe和大家说千万不要忘了要return cpuAddress,这个是非常重要的。没有这个我们就得不到这个值。那我们做的就白费了。虽然代码不是很难,但是我还是拿出来给大家分享一下。