java System.getProperty(param) 返回错误值 - Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16944494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:27:31  来源:igfitidea点击:

System.getProperty(param) returns wrong value - Android

javaandroidshell

提问by amirelouti

steps I do:

我做的步骤:

I do in code

我在代码中做

System.setProperty("myproperty", 1);

and then I set in a shell script the property "myProperty" to 3. like this:

然后我在 shell 脚本中将属性“myProperty”设置为 3。像这样:

# setprop "myproperty" 3

and then in the code I try to read the property like this:

然后在代码中,我尝试像这样读取属性:

System.getProperty("myproperty");

I get the value of 1. which means that the set from the shell didn't actually work. but when I print all props from shell with

我得到的值为 1。这意味着 shell 中的设置实际上不起作用。但是当我从 shell 打印所有道具时

# getprop

I see in the list that myproperty equals 3.

我在列表中看到 myproperty 等于 3。

in shorter words: I want to change the value of a property from a script, and I see that this scripts actually changes the property but in the java code I get the old value.

简而言之:我想从脚本中更改属性的值,我看到这个脚本实际上更改了属性,但在 java 代码中我得到了旧值。

any ideas?

有任何想法吗?

回答by Juned Ahsan

Java code in Android provides System.getProperty and System.setProperty functions in java library but it's important to note that although these java APIs are semantically equal to native version, java version store data in a totally different place. Actually, a hashtable is employed by dalvik VM to store properties. So, java properties are separated, it can't get or set native properties, and neither vice versa.

Android 中的 Java 代码在 java 库中提供了 System.getProperty 和 System.setProperty 函数,但需要注意的是,虽然这些 java API 在语义上与原生版本相同,但 java 版本将数据存储在完全不同的地方。实际上,dalvik VM 使用哈希表来存储属性。所以,java属性是分开的,它不能获取或设置本地属性,反之亦然。

You can use android.os.SystemProperties class can manipulate native properties, though it's intended for internal usage only. It calls through jni into native property library to get/set properties.

您可以使用 android.os.SystemProperties 类来操作本机属性,尽管它仅供内部使用。它通过 jni 调用本地属性库来获取/设置属性。

回答by njzk2

getprop/setprop work on android.os.SystemProperties, not on java.lang.System.

getprop/setprop 作用于android.os.SystemProperties,而不是作用于java.lang.System

Unfortunately, this class is not available to third party application. Apparently you have rooted your device, so you may still access it.

不幸的是,此类对第三方应用程序不可用。显然你已经植根了你的设备,所以你仍然可以访问它。

回答by Muzikant

You can use that snippet to run getPropas shell command and get the value of any property:

您可以使用该代码段getProp作为 shell 命令运行并获取任何属性的值:

private String getSystemProperty(String propertyName) {
    String propertyValue = "[UNKNOWN]";

    try {
        Process getPropProcess = Runtime.getRuntime().exec("getprop " + propertyName);

        BufferedReader osRes =
                new BufferedReader(new InputStreamReader(getPropProcess.getInputStream()));

        propertyValue = osRes.readLine();

        osRes.close();
    } catch (Exception e) {
        // Do nothing - can't get property value
    }

    return propertyValue;
}