34

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

Jonas
  • 121,568
  • 97
  • 310
  • 388
Sébastien
  • 13,831
  • 10
  • 55
  • 70
  • 1
    There is presently no way to truly disable network access programmatically from within a unit test. I filed an [enhancement request](http://code.google.com/p/android/issues/detail?id=36916) for this issue. – Jeff Axelrod Aug 31 '12 at 13:33
  • 1
    @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. – class stacker Jan 17 '13 at 10:20

11 Answers11

35

WIFI:

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

MOBILE DATA:

$ adb shell svc data enable
$ adb shell svc data disable
Marco C.
  • 1,282
  • 2
  • 15
  • 19
  • I've burnt two hours trying to get this figured out. Thank you sir! – fmcauley Jul 15 '16 at 00:58
  • 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); } } – krawiec Dec 16 '16 at 14:04
  • 6
    One liners for copy pasters: `adb shell "svc wifi disable && svc data disable"` `adb shell "svc wifi enable && svc data enable"` – giorgos.nl May 20 '20 at 14:59
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.

James Koch
  • 440
  • 4
  • 8
  • 1
    This answer does not solve the problem. See my answer for a way to truly run an offline AVD. – class stacker Jan 04 '13 at 14:48
  • 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. – steven Jan 27 '15 at 21:12
  • 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. – Saifur Rahman Mohsin Jul 02 '15 at 13:33
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.

noamtm
  • 12,435
  • 15
  • 71
  • 107
  • 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 eth0 – Adam Burley Aug 03 '21 at 17:01
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...

Jercik
  • 71
  • 2
  • 1
    pressing F8 shows that you are not connected, but you are! Network connections continue work after disconnecting – steven Jan 27 '15 at 21:04
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.

Nick Vallely
  • 1,396
  • 1
  • 13
  • 18
  • 1
    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... – Sébastien Oct 24 '10 at 19:34
  • *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? – Nick Vallely Oct 26 '10 at 18:05
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.

Lenciel
  • 553
  • 3
  • 13
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.

diptia
  • 2,135
  • 21
  • 21
0

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

Comment 15 by iscy%inv...@gtempaccount.com, 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 leonhard...@gmail.com, 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...

Marvin Emil Brach
  • 3,984
  • 1
  • 32
  • 62
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.

class stacker
  • 5,357
  • 2
  • 32
  • 65
  • 3
    This will indeed put the device into offline mode, but then its so much offline that not even adb can connect to it... – Thomas Keller May 27 '13 at 09:35
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

lance.dolan
  • 3,493
  • 27
  • 36
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.

PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
  • That's not the same as no network, which is a detectable phone state. – Walf Mar 08 '12 at 05:21
  • 1
    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](http://stackoverflow.com/privileges/vote-down): `Downvoting should be reserved for extreme cases.`, `If something is wrong, please leave a comment or edit the post to correct it` – PiTheNumber Mar 08 '12 at 07:00