How to read Android properties with Java
Asked Answered
K

5

13

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

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:

[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.

Any ideas?

Koblenz answered 26/1, 2015 at 20:28 Comment(1)
you would need access to this class android.googlesource.com/platform/frameworks/base.git/+/…Vanzandt
J
6

System.getProperties() does not return the same properties as getprop.

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

Jungian answered 26/1, 2015 at 20:36 Comment(1)
I am aware of how to iterate. I am basically looking for the properties that contains the dolby info (if available) on device. It is not System.getProperties() from what I can see.Koblenz
C
9

I cleaned up TMont's solution and made it more generic (added parameter for 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();
            }
        }
    }
}
Chapple answered 13/1, 2017 at 13:25 Comment(0)
K
7

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();
    }
Koblenz answered 26/1, 2015 at 21:40 Comment(1)
How would you modify this to pass "dolby" as an argument? (Or exact prop?)Crossexamine
J
6

System.getProperties() does not return the same properties as getprop.

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

Jungian answered 26/1, 2015 at 20:36 Comment(1)
I am aware of how to iterate. I am basically looking for the properties that contains the dolby info (if available) on device. It is not System.getProperties() from what I can see.Koblenz
M
6

There actually is a system side implementation of the getprop call. 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.

Maryleemarylin answered 5/3, 2020 at 9:24 Comment(0)
D
5

For getting the system properties using reflection, try the below code snippet with your required property name.

public String getSerialNumber() {
        String serialNumber;

        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class);

            serialNumber = (String) get.invoke(c, "ril.serialnumber");

            if (serialNumber.equals(""))
                serialNumber = (String) get.invoke(c, "ro.serialno");
            if (serialNumber.equals(""))
                serialNumber = (String) get.invoke(c, "ro.boot.serialno");
            if (serialNumber.equals(""))
                serialNumber = (String) get.invoke(c, "ro.kernel.androidboot.serialno");
            // If none of the methods above worked
            if (serialNumber.equals("")) {
                serialNumber = Build.SERIAL;
            }
        } catch (Exception e) {
            e.printStackTrace();
            serialNumber = "";
        }

        return serialNumber;
    }
Duodenitis answered 14/10, 2020 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.