可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
回答1:
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.
回答2:
WIFI:
$ adb shell svc wifi enable
$ adb shell svc wifi disable
MOBILE DATA:
$ adb shell svc data enable
$ adb shell svc data disable
回答3:
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.
回答4:
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...
回答5:
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.
回答6:
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.
回答7:
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.
回答8:
Old questions, but I found a solution:
- Open a command line: CTRL+R >
cmd
Change to android tools folder
cd C:\Program Files\Android\android-sdk\tools
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.
回答9:
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...
回答10:
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.
回答11:
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:
Then, adjust your cellular settings, "Signal Strength" and "Data Status" to "none" and "denied" respectively, as shown in this image: