gpt4 book ai didi

java - 如何使用 Java 读取 Android 属性

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:29:24 26 4
gpt4 key购买 nike

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

我试过几种方法,例如:

Properties sysProps = System.getProperties();

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

adb shell getprop | grep dolby

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]

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

有什么想法吗?

最佳答案

我清理了 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();
}
}
}
}

关于java - 如何使用 Java 读取 Android 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28158175/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com