Android 获取SN序列号

一、命令行:
测试代码:

    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            Log.d("GetSnNumbers", "SN: " + msg.obj.toString());
        }
    };
    Thread snThread = new Thread(new getSerialnoNumbers(mHandler));
    snThread.start();

 

类:

    private static class getSerialnoNumbers implements Runnable {
        Handler mHandler;
        final String serialnoStr = "[ro.boot.serialno]";

        getSerialnoNumbers(Handler handler) {
            this.mHandler = handler;
        }

        public void run() {
            try {
                Process p = Runtime.getRuntime().exec("getprop");
                p.waitFor();
                BufferedReader stdInput = new BufferedReader(new InputStreamReader(
                        p.getInputStream()));
                String temp = "";
                while ((temp = stdInput.readLine()) != null) {
                    if (temp.contains(serialnoStr)) {
                        temp.replaceAll(" ", "");
                        int index = temp.indexOf(serialnoStr);
                        temp = temp.substring(index + 20);
                        temp = temp.substring(1, temp.length() - 1);
                        Log.d("getSerialnoNumbers", temp);
                        Message msg = new Message();
                        msg.obj = temp;
                        mHandler.sendMessage(msg);
                        break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

 

二、反射:

测试代码:

    String[] propertys = {
        "ro.boot.serialno", "ro.serialno"
    };

    for (String key : propertys) {
        // String v = android.os.SystemProperties.get(key);
        String v = getAndroidOsSystemProperties(key);
        Log.e("", "get " + key + " : " + v);
    }

 

方法:

    static String getAndroidOsSystemProperties(String key) {
        Method systemProperties_get = null;
        String ret;
        try {
            systemProperties_get = Class.forName("android.os.SystemProperties").getMethod("get",
                    String.class);
            if ((ret = (String) systemProperties_get.invoke(null, key)) != null)
                return ret;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return "";
    }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。