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 "";
    }

 

posted @ 2014-11-04 13:35  挽风阁  阅读(8669)  评论(0编辑  收藏  举报