码迷,mamicode.com
首页 > 移动开发 > 详细

Android 获取SN序列号

时间:2014-11-04 14:56:12      阅读:654      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   io   color   ar   os   for   sp   

一、命令行:
测试代码:

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

 

Android 获取SN序列号

标签:android   style   blog   io   color   ar   os   for   sp   

原文地址:http://www.cnblogs.com/ren-gh/p/4073493.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!