How to define and use a system property in Android Instrumentation test?

前端 未结 7 1039
时光说笑
时光说笑 2020-12-02 23:25

I am trying to use some arguments for an Instrumentation test. I noticed that I can read system properties with System.getProperty() function. So I use setprop

7条回答
  •  感动是毒
    2020-12-02 23:56

    To get the property set by 'setprop', there are two options:
    One. use android.os.SystemProperties, this is a hide API. use it like this:

    Class clazz = null;
    clazz = Class.forName("android.os.SystemProperties");
    Method method = clazz.getDeclaredMethod("get", String.class);
    String prop = (String)method.invoke(null, "AP");
    Log.e("so_test", "my prop is: <" + prop  + ">");
    

    Two. use 'getprop' utility:

    Process proc = Runtime.getRuntime().exec(new String[]{"/system/bin/getprop", "AP"});
    BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    Log.e("so_test", "my prop is: " + reader.readLine());
    

    Maybe using functions availble in NDK is an option too, but why bother?

提交回复
热议问题