小编tru*_*ung的帖子

无法在Android中使用Reflection或Linux命令exec在Android中设置系统属性

我需要设置并获取名为"persist.sys.aabbcc"的系统属性.我能够使用adb shell命令读取/写入值,如下所示:

adb shell setprop persist.sys.aabbcc 123456
Run Code Online (Sandbox Code Playgroud)

和:

adb shell getprop persist.sys.aabbcc 
123456
Run Code Online (Sandbox Code Playgroud)

我还可以使用Reflection在java Android中读取此属性:

        @SuppressWarnings("rawtypes")
        Class SystemProperties = Class.forName("android.os.SystemProperties");

        //Parameters Types
        @SuppressWarnings("rawtypes")
        Class[] paramTypes = new Class[1];
        paramTypes[0] = String.class;

        Method get = SystemProperties.getMethod("get", paramTypes);

        //Parameters
        Object[] params = new Object[1];
        params[0] = new String("persist.sys.aabbcc");

        ret = (String) get.invoke(SystemProperties, params);
Run Code Online (Sandbox Code Playgroud)

或者使用Linux命令exec:

            try
            {              
                String line;
                java.lang.Process p = Runtime.getRuntime().exec("getprop persist.sys.aabbcc");              
                BufferedReader input = new BufferedReader(new   InputStreamReader(p.getInputStream()));
                while ((line = input.readLine()) != null)
               {
                    System.out.println(line);
                    Log.d("HelloDroid", line);           
                }
                input.close();
            } …
Run Code Online (Sandbox Code Playgroud)

android

6
推荐指数
2
解决办法
2万
查看次数

标签 统计

android ×1