get CPU Serial - Android Hardware

Android examples for Hardware:CPU Information

Description

get CPU Serial

Demo Code


//package com.java2s;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class Main {

    private static String getCPUSerial() {
        String str = "", strCPU = "", cpuAddress = "0000000000000000";
        try {/*  w  w  w.  j  a  v  a2 s .co m*/
            // ????CPU????
            Process pp = Runtime.getRuntime().exec("cat /proc/cpuinfo");
            InputStreamReader ir = new InputStreamReader(
                    pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            // ??CPU??????
            while (str != null) {
                str = input.readLine();
                System.out.println(str);
                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;
    }
}

Related Tutorials