如何定义和Android的仪器测试使用系统属性? [英] How to define and use a system property in Android Instrumentation test?

查看:174
本文介绍了如何定义和Android的仪器测试使用系统属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一些参数进行了仪器测试。我注意到,我可以读取系统属性与 System.getProperty()功能。所以我用setprop命令来设置系统属性。例如:亚行外壳setprop AP 123 。 在我的测试code我尝试读取这个美联物业:

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

然后,我用的logcat查看此调试消息,但我得到这个属性为空值。什么任何想法可能是错误的? 请注意,我仍然可以读取系统属性与亚行外壳getprop AP 命令。

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.

推荐答案

要获得通过'setprop设置属性,有两种选择:
一。使用android.os.SystemProperties,这是一个隐藏的API。使用这样的:

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  + ">");

二。使用'getprop'工具:

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

也许使用的功能在availble的NDK是一个选项太多,但何必呢?

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

这篇关于如何定义和Android的仪器测试使用系统属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆