如何避免monkey压测关闭wifi

@TOCmonkey压测问题

如何避免monkey压测关闭wifi——自动开启wifi

最近有一个需求是使用monkey 压力测试批量的安卓apk,获取apk运行时产生的流量。然而,常常没跑几个,monkey就把wifi关了。。。我也是醉醉哒

设备:一台没有装sim卡的华为手机

一、试图尝试过的方法

  1. 隐藏虚拟键及顶部状态栏:
adb shell settings put global policy_control immersive.full=*

这个方法失败了,我猜是因为只是隐藏了状态栏,但是monkey依然会将其触发(点击)出来,然后(不小心)关闭我的wifi。

  1. 将手机root后,直接使用下述命令开启
adb shell svc wifi disable
adb shell svc wifi enable

这个方法我直接放弃了,root手机太麻烦了,先试试别的。

二、编写可以检测wifi状态的apk

借鉴了里面的代码和命令

https://www.jianshu.com/p/4e3f80adaeb1

1.使用android studio编写上述链接中的apk——wifi_on.apk,安装在华为手机上。
2.检测wlan0是否正常(wifi是否成功连上)

def check_wifi():
    cmd = 'adb shell ifconfig wlan0'
    r = os.popen(cmd)
    text = r.read()
    if 'RUNNING' in text:
        return True
    else:
        return False
  1. 检测wifi_on,apk是否正常允许
def check_if_apk_run():
    cmd_if_run = ' adb shell ps | findstr *** ' # ***是wifi_on.apk的包名
    r = os.popen(cmd_if_run)
    text = r.read()
    if len(text)> 0 :
        return True
    else:
        cmd_start = 'adb shell am start -W ***/.MainActivity' #命令行启动wifi_on,-W等待启动成功
        r = os.popen(cmd_start)
        text = r.read()
        if 'Complete' in text:
            return True
        else:
            return False

4.当wifi没有连接,并且wifi_on正在运行,使用下述命令向wifi_on传递intent com.example.wifiON 来开启wifi

adb shell am broadcast -a com.example.wifiON -n ***/.WifiControllerReceiver

总结

到这里基本就算完成了。
你以为成功了吗?并没有,至少对于我来说并没有。华为在获取权限时出现了下述提示:
在这里插入图片描述
。。。
查了一圈解决方案,好像还是需要root,用svc wifi命令实现方可绕过华为的这个检测。
我是谁我在哪儿我要做什么,噢我要root手机,噢不,我选择人工允许。

经过测试,华为、三星均出现了上述问题,vivo未出现。

2019年3月11日补充:
三星经过下述调试可解决需手动授权的问题。
在这里插入图片描述