How to disable/enable network, switch to Wifi in Android emulator?
Solution 1
Right click on project in the "Project Explorer" panel, chose "Run As" then "Run Configurations".
On the left side of the modally shown window chose "Android Application" and on the right side of the same window, chose tab "Target".
At the bottom of the window, in the "Emulator Launch Parameters" section you have plethora of options regarding internet connectivity.
I hope this helped.
Igor
Solution 2
I've found the Android Dev Tools app really helpful.
It allows you to manipulate important system settings like network connectivity. It even allows you to set an interval in which it toggles wifi on and off automatically thus allowing you to test you network related broadcast receivers.
If you'd like to install the Dev Tools application on a real development device, you can copy the application from your emulator and then install it on your device using ADB. To copy the application from a running emulator, execute:
adb -e pull /system/app/Development.apk ./Development.apk
This copies the .apk file into the current directory. Then install it on your connected device with:
adb -d install Development.apk
Solution 3
Unfortunately, there is no way to truly disable network access from within a unit test programmatically. I filed an Android enhancement request for this issue.
Solution 4
You might want to look at the emulator control view in eclipse too. I'm not having a lot of luck getting it to kill my data at the moment, but it looks like it should.
mtbkrdave
Updated on March 23, 2020Comments
-
mtbkrdave over 4 years
I'm working on a Push Notifications library for Android (http://deaconproject.org/) that needs to take action if network connectivity is interrupted or changed - namely, it needs to re-initiate a server connection or pause its operation until network connectivity is available. This seems to work fine using and Android BroadcastReceiver for "android.net.ConnectivityManager.CONNECTIVITY_ACTION".
My problem is in testing the library - I would like to automatically test the library's response to a broken network connection, or a transition from 3G to WiFi, under various configuration conditions. The problem is, I don't want to sit with the emulator and hit F8 all day.
Is there a way to programmatically manipulate network connections on Android from within a jUnit test without resorting to toggling Airplane Mode? I've already tried issuing commands to the emulator via the console, manipulating the GSM mode, etc, but while the phone state changes on the display, the Internet connection remains up.
-
mtbkrdave almost 14 yearsThank you for your answer - This is a step in the right direction. The emulator launch parameters can adjust network connectivity at boot time, but we're looking for a way to do that programmatically, at runtime. The library "under test" must respond to Android Broadcast Intents (such as android.net.ConnectivityManager.CONNECTIVITY_ACTION) in order to adjust its state to the network state of the device. What we need is a way to change the network state from within the JUnit test, so that we can verify that the Intents are received and acted on appropriately...
-
mtbkrdave almost 14 yearsI have tried this option, though through the console ADB connection, but it doesn't seem to actually remove connectivity; despite my switching off the GSM data connection, network traffic still works. What I'm looking for here is a way to do it programmatically - within the scope of an automated test, such as an JUnit test.
-
Chris almost 14 yearsI've had the same problem (communication seems to continue). It sounds like you've found a way to script this via ADB shell (should it start working again or we stumble across the proper control commands). FYI I've also been able to toggle some network settings (same problem exists though) via telnet to the emu and the gsm data off and gsm voice off commands ala stackoverflow.com/questions/2577785/…
-
mtbkrdave almost 13 yearsThis is helpful for those who wish to manipulate the network state manually, but the question here is looking for a way to do so programmatically (i.e. from within a unit test). Could this be done using an intent sent to the Dev Tools application?
-
Walf over 12 yearsThanks for the hint, changing those statuses to
unregistered
worked for me. No idea about unit tests, though.