How to put an AVD (emulator) into OFFLINE mode?
Asked Answered
L

11

35

I would like to test the behavior of my application when the user is in a no-signal zone or when his phone is in flight mode.

-> How do I put an Android Virtual Device into offline mode (where no connection to the internet is possible) ?

I have tried the following:

  • Activating flight mode via the AVD settings: surprisingly, this does NOT work (browsing the internet is still possible in flight mode!)

  • Disabling the network connection on my PC: this obviously works, but is not satisfactory as I need Internet when I am testing my app

Lula answered 24/10, 2010 at 14:18 Comment(2)
There is presently no way to truly disable network access programmatically from within a unit test. I filed an enhancement request for this issue.Lighten
@Sebastien I have provided an answer below which works perfectly well while unfortunately disconnecting the AVD completely. Still, you can see how your application behaves.Iphlgenia
S
14

I'm seeing all the same problems. DDMS doesn't fully kill it. Neither does F8. And neither does turning on airplane mode inside the emulator. We should all probably star this issue someone logged in 2009.

Sparkle answered 18/1, 2011 at 21:59 Comment(3)
This answer does not solve the problem. See my answer for a way to truly run an offline AVD.Iphlgenia
in meantime they closed the issue without fixing it. But we should star it more and more because it still exists in 5.0.1! It seems to be the case that the best workaround is not to use the emulator and test on a real device.Afraid
It’s 2015 and I still face this issue for some reason on my default Nexus 5 x86 emulator that downloads on Android Studio installation.Cornellcornelle
S
35

WIFI:

$ adb shell svc wifi enable
$ adb shell svc wifi disable

MOBILE DATA:

$ adb shell svc data enable
$ adb shell svc data disable
Swint answered 26/1, 2016 at 14:40 Comment(3)
I've burnt two hours trying to get this figured out. Thank you sir!Epp
It works from adb command line, but when I try it in the app, it does not work and doesn't print any error logs. Do you have any idea, why it is not working when invoking directly from the app using ProcessBuilder? public void disableMobileData(){ ProcessBuilder pb = new ProcessBuilder("svc", "data", "disable"); runCommand(pb); } public void runCommand(ProcessBuilder pb){ try { Process pc = pb.start(); pc.waitFor(); } catch (Exception e){ Log.e(TAG,"Exception"+e); } }Incunabulum
One liners for copy pasters: adb shell "svc wifi disable && svc data disable" adb shell "svc wifi enable && svc data enable"Pauly
S
14

I'm seeing all the same problems. DDMS doesn't fully kill it. Neither does F8. And neither does turning on airplane mode inside the emulator. We should all probably star this issue someone logged in 2009.

Sparkle answered 18/1, 2011 at 21:59 Comment(3)
This answer does not solve the problem. See my answer for a way to truly run an offline AVD.Iphlgenia
in meantime they closed the issue without fixing it. But we should star it more and more because it still exists in 5.0.1! It seems to be the case that the best workaround is not to use the emulator and test on a real device.Afraid
It’s 2015 and I still face this issue for some reason on my default Nexus 5 x86 emulator that downloads on Android Studio installation.Cornellcornelle
A
7

I found a working solution. Tested on Android 6.0 an 7.0 emulators, but should work on others.

Network down:

adb shell ifconfig eth0 down

Network up:

adb shell ifconfig eth0 up

If you get a "Permission denied" error, restart adb daemon as root:

adb root

and try again.

Adhesion answered 27/9, 2016 at 12:53 Comment(1)
wow - after reading 15+ answers across multiple threads this is the only one which worked. although I needed to use adb shell ifconfig wlan0 down rather than eth0Apteral
G
3

You should just press F8 in emulator window, it toggle off cell networking according to: http://developer.android.com/guide/developing/tools/emulator.html. But I test it and it is not working too! There is only message that there is no internet connection but it actually works...

Giron answered 24/10, 2010 at 16:45 Comment(1)
pressing F8 shows that you are not connected, but you are! Network connections continue work after disconnectingAfraid
W
2

All I do is go into the Window menu, select 'open perspective->other' open the DDMS perspective. Inside the DDMS perspective you have an 'Emulator Control' view where you can adjust latency, roaming, you can give it different telephone and data states. So following this procedure to get the Emulator view, you then go to 'Data' drop down and select the 'Denied' option.

Woolridge answered 24/10, 2010 at 16:0 Comment(2)
This is indeed the kind of control I was looking for. However, it does NOT work either... I set Voice and Data to denied, but I am still am able to browse the internet from the AVD. This definitely looks like a bug in the emulator...Jacobo
It works on my box :) I imagine there may be something else I have set that you do not. When I get a chance I will dig into it and see if I can reproduce it not working... what version of eclipse/avd?Woolridge
O
2

I found in my 'Emulator Control' view when I adjust Voice and Data to "unregistered" not "denied" works for me. Now I finally got the AVD offline for network while I can still post this answer.

Ochrea answered 23/1, 2011 at 13:42 Comment(2)
Does not work for me: my app can still download data from the web.Jacobo
This is only usefull to change the state, but its still able to connect to internet.Automobile
R
2

Instead of using Airplane mode in the Android emulator ( which does not work too well ) , switch off the network connectivity to your system while testing. That is , switch of the Ethernet connectivity or Wi-fi . Worked well for me.

Rightwards answered 23/3, 2012 at 14:4 Comment(0)
S
0

Old questions, but I found a solution:

  1. Open a command line: CTRL+R > cmd
  2. Change to android tools folder

    cd C:\Program Files\Android\android-sdk\tools

  3. Run the emulator with a false DNS server. Change 2.3.3 to the the name of your AVD. The @ makes it start:

    emulator.exe @2.3.3 -dns-server 127.0.0.1

Check the manual for more command line options.

Suziesuzuki answered 1/2, 2012 at 15:54 Comment(2)
That's not the same as no network, which is a detectable phone state.Bennion
There is always someone how wants more... I guess you can switch on flight mode to change the state. Also you can of course, still make a connection using the ip instead of a domain name. But for most applications this will cut of the internet connection which is more than any other answer here does. Please read the FAQ about down voting: Downvoting should be reserved for extreme cases., If something is wrong, please leave a comment or edit the post to correct itSuziesuzuki
E
0

One comment of the issue mentioned by James Koch says that there is an workaround:

Comment 15 by iscy%[email protected], Jul 8, 2011
Finally found a work around for this issue:
telnet localhost 5554    // Connects to the emulator
qemu monitor             // Enter in the QEmu functionalities
info network             // List the network adapter "user.0" is usually the name
set_link user.0 down     // Shut the network down
set_link user.0 up       // Re-enable networking

BUT:

Comment 16 by [email protected], Sep 6, 2011
This work around doesn't work for me on MacOS 10.7.1 and SDK rev 12 (with platform 2.2, 2.1 and 2.3.3)
... ...
qemu monitor
KO: internal error: could not detach from console !

-> Same for me on Windows Vista, SDK rev 19 and platform 10 (2.3.3)

But perhaps that's usable for somebody...

Endocarp answered 31/7, 2012 at 10:6 Comment(0)
I
0

Finally, how about this approach.

android-sdks/tools/emulator -avd [AVD name goes here] -qemu -net nic,vlan=0 -net user,vlan=1

Actually, this is a trick because the following crashes my emulator:

android-sdks/tools/emulator -avd [AVD name goes here] -qemu -net none

Tested with Android emulator V21.0 on Debian.

Iphlgenia answered 4/1, 2013 at 14:46 Comment(1)
This will indeed put the device into offline mode, but then its so much offline that not even adb can connect to it...Falls
T
0

I found that the answer from @Marco C. worked, but I also found that my AVD settings allowed for doing this very easily as well.

In the sidebar, click the triple dot icon all the way at the bottom:

enter image description here

Then, adjust your cellular settings, "Signal Strength" and "Data Status" to "none" and "denied" respectively, as shown in this image:

enter image description here

Thyestes answered 8/3, 2019 at 18:24 Comment(1)
This simply does not work. There's still full internet access.Politic

© 2022 - 2024 — McMap. All rights reserved.