如何使用 Java 读取 Android 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28158175/
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-02 13:06:03  来源:igfitidea点击:

How to read Android properties with Java

javaandroidshelladbgetproperties

提问by TMont

I use 'adb shell getprop' in the terminal. What interfaces can I use in Android JAVA to get the same information?

我在终端中使用“adb shell getprop”。我可以在Android JAVA中使用哪些接口来获取相同的信息?

I have tried several things like:

我尝试了几件事,例如:

Properties sysProps = System.getProperties();

But I don't think these are the same properties I am looking for? Specifically, I want to find values that will return similar to the following:

但我不认为这些是我正在寻找的相同属性?具体来说,我想找到将返回类似于以下内容的值:

adb shell getprop | grep dolby

The shell 'grep dolby' command returns this:

shell 'grep dolby' 命令返回:

[audio.dolby.ds2.enabled]: [true] 
[dolby.audio.sink.info]: [headset] 
[dolby.ds.dialogenhancer.state]: [on] 
[dolby.ds.graphiceq.state]: [off] 
[dolby.ds.hpvirtualizer.state]: [off] 
[dolby.ds.intelligenteq.preset]: [Off] 
[dolby.ds.intelligenteq.state]: [off] 
[dolby.ds.platform]: [qcom] 
[dolby.ds.profile.name]: [Movie] 
[dolby.ds.spkvirtualizer.state]: [off] 
[dolby.ds.state]: [off] 
[dolby.ds.volumeleveler.state]: [on] 

But I want to access this information in Android JAVA code.

但我想在 Android JAVA 代码中访问这些信息。

Any ideas?

有任何想法吗?

采纳答案by George

System.getProperties() does notreturn the same properties as getprop.

System.getProperties()并没有为getprop返回相同的属性。

To get getprop properties, try executing getprop using Runtime.exec() and reading its standard output.

要获取 getprop 属性,请尝试使用 Runtime.exec() 执行 getprop 并读取其标准输出。

回答by Florian Bramer

I cleaned up TMont's solution and made it more generic (added parameter for propertyName):

我清理了 TMont 的解决方案并使其更通用(为 propertyName 添加参数):

import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SystemProperties {

    private static String GETPROP_EXECUTABLE_PATH = "/system/bin/getprop";
    private static String TAG = "MyApp";

    public static String read(String propName) {
        Process process = null;
        BufferedReader bufferedReader = null;

        try {
            process = new ProcessBuilder().command(GETPROP_EXECUTABLE_PATH, propName).redirectErrorStream(true).start();
            bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = bufferedReader.readLine();
            if (line == null){
                line = ""; //prop not set
            }
            Log.i(TAG,"read System Property: " + propName + "=" + line);
            return line;
        } catch (Exception e) {
            Log.e(TAG,"Failed to read System Property " + propName,e);
            return "";
        } finally{
            if (bufferedReader != null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {}
            }
            if (process != null){
                process.destroy();
            }
        }
    }
}

回答by TMont

In case someone wanted to know my solution... with George's help I ended up using this:

如果有人想知道我的解决方案......在乔治的帮助下,我最终使用了这个:

private String propReader() {
        Process process = null;
        try {
            process = new ProcessBuilder().command("/system/bin/getprop")
                    .redirectErrorStream(true).start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        InputStream in = process.getInputStream();

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

        StringBuilder log = new StringBuilder();
        String line;
        try {
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains("dolby"))
                    log.append(line + "\n");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println(in);

        process.destroy();
        return log.toString();
    }

回答by Xerusial

There actually is a system side implementation of the getpropcall. It is called Systemproperties.get()and can be found here. For users, that work on system code inside the AOSP, or do want to take the risk of using reflect, this is the way to go.

实际上有getprop调用的系统端实现。它被称为Systemproperties.get()并且可以在这里找到。对于在 AOSP 内部处理系统代码的用户,或者确实想冒险使用reflect,这是要走的路。