如何阅读与Java的Android属性

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

我已经尝试了几件事情:

Properties sysProps = System.getProperties(); 

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

 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代码中访问这些信息。

有任何想法吗?

System.getProperties()不会返回与getprop相同的属性。

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

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

 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(); } 

我清理了TMont的解决scheme,并使其更通用(为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(); } } } }