Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

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

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 command to set a system property. For example: adb shell setprop AP 123. Inside my Test code I try to read this AP property with :


tmp = System.getProperty("AP"); 
Log.d("MyTest","AP Value = " + tmp);

Then I use logcat to view this debug message but I get a null value for this property. Any ideas on what could be wrong? Note that I can still read the system property with adb shell getprop AP command.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...