如何在我的uiautomator项目中调用像wifi_service这样的android系统服务?

时间:2015-07-26 16:52:45

标签: java android uiautomator autotest android-uiautomator

public String getMacInfo(){    
    String mac = "";    
       WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);    
       WifiInfo wifiInfo = wifiManager.getConnectionInfo();    
       if(wifiInfo.getMacAddress()!=null){    
        mac = wifiInfo.getMacAddress();    
    } else {    
        mac = "Fail";    
    }    

       return mac;    
}   

我在测试用例中添加了这些代码,出现了错误

显示在" mContext"第3行。

谁能帮助我?

谢谢!

1 个答案:

答案 0 :(得分:1)

" mContext无法解析"错误意味着变量" mContext"不在范围内(或未在任何地方声明)。

修复很简单,您只需要将对象声明为成员变量,并在setUp()方法中初始化它。

public class MyTest extends InstrumentationTestCase {
    private Context mContext;

    public void setUp() {
        mContext = getInstrumentation().getContext();
    }

    // ...
}

有关此特定错误的类似问题,请参阅Java, "Variable name" cannot be resolved to a variable