• 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 "";
        }
  • 相关阅读:
    原创 C++应用程序在Windows下的编译、链接(四)动态链接
    IE浏览器 json异常
    Linux系统github使用
    Mysql in 排序
    转 php四种基础算法:冒泡,选择,插入和快速排序法
    转 mysql取今天,明天,工作日,周末,本周,下周,下月数据
    下载远程图片到本地
    转 PHP中SQL_CALC_FOUND_ROWS与FOUND_ROWS()和count()
    星级点评
    21个值得收藏的Javascript技巧
  • 原文地址:https://www.cnblogs.com/ren-gh/p/4073493.html
Copyright © 2020-2023  润新知