zoukankan      html  css  js  c++  java
  • 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 "";
        }
  • 相关阅读:
    Beyond Compare保存快照和CRC比较相结合的方法
    如何在Beyond Compare文本比较时设置书签
    如何使用Navicat for SQLite 触发器
    Navicat Premium 中实用工具介绍
    Beyond Compare查看合并文本后相同内容的方法
    Marriage Match II HDU
    Escape HDU
    kebab HDU
    Task Schedule HDU
    网络流深入
  • 原文地址:https://www.cnblogs.com/ren-gh/p/4073493.html
Copyright © 2011-2022 走看看