Programmatically Controlling WiFi for Stable Android Monkey Testing
When running unattended Android monkey tests, unexpected WiFi shutdowns can disrupt results, so this guide shows how to build a lightweight Java app that checks WiFi status every minute and toggles it every ten minutes using ADB commands to keep the network stable.
During automated Android monkey testing, the device’s WiFi may be turned off or flight mode enabled, causing test failures even though monkey supports offline testing. To maintain a stable network while the tests run unattended, the author created a small Android application whose sole purpose is to monitor and switch the WiFi state.
Solution Overview
The app runs a background thread that:
Checks the current WiFi status against the expected state every minute.
Every ten minutes flips the expected WiFi state, creating a cross‑testing scenario.
Uses adb shell ifconfig wlan0 to read the actual WiFi status.
Executes adb shell am start -n run.wifibutton/.WifiButtonActivity to toggle WiFi via an ADB command.
Key Code
package monkeytest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import source.Common;
public class WifiSwitch extends Thread {
public static boolean WIFIKEY = true; // thread control flag
public static boolean WIFISTATUS = true; // desired WiFi state
@Override
public void run() {
while (WIFIKEY) {
for (int i = 0; i < 10; i++) {
if (!WIFIKEY) break;
Common.getInstance().sleep(60 * 1000);
keepWifiONorOFF(WIFISTATUS);
}
WIFISTATUS = !WIFISTATUS; // invert expected state
}
}
/** Stop the thread */
public void stopWifiSwitch() {
WIFIKEY = false;
}
/** Toggle WiFi via ADB */
public void closeOrOpenWifi() {
try {
Runtime.getRuntime().exec(Common.ADB_PATH + "adb shell am start -n run.wifibutton/.WifiButtonActivity").waitFor();
} catch (InterruptedException | IOException e) {
Common.getInstance().output("WiFi toggle error!", e);
}
}
/** Keep WiFi in the expected state */
public void keepWifiONorOFF(boolean status) {
if (status & wifiIsOn()) {
closeOrOpenWifi();
}
}
/** Determine whether WiFi is currently on */
private boolean wifiIsOn() {
String cmd = "adb shell ifconfig wlan0";
System.out.println("Executing: " + cmd);
String osName = System.getProperty("os.name");
try {
Process p = osName.contains("Mac") ?
Runtime.getRuntime().exec(Common.ADB_PATH + cmd) :
Runtime.getRuntime().exec("cmd /c " + cmd);
InputStream input = p.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("RUNNING")) {
return true;
}
}
reader.close();
input.close();
} catch (IOException e) {
Common.getInstance().output("Failed to execute " + cmd);
e.printStackTrace();
}
return false;
}
}How It Works
The thread continuously loops while WIFIKEY is true. Inside the loop it sleeps for one minute, then calls keepWifiONorOFF with the current desired state. After ten iterations (ten minutes) the desired state is inverted, causing the next cycle to enforce the opposite WiFi condition.
The method wifiIsOn runs the ifconfig wlan0 command and checks the output for the keyword RUNNING, which indicates that the WiFi interface is active. If the actual state does not match the expected one, closeOrOpenWifi launches the helper activity run.wifibutton/.WifiButtonActivity via ADB to toggle the WiFi.
Usage
Compile the class as part of a test utility project, ensure Common.ADB_PATH points to the correct ADB executable, and start the thread before launching the monkey test. The thread will automatically keep the WiFi state aligned with the test schedule, reducing flaky failures caused by unexpected network changes.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
