如何禁用/启用网络,在 Android 模拟器中切换到 Wifi?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3002886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable/enable network, switch to Wifi in Android emulator?
提问by mtbkrdave
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".
我正在为 Android ( http://deaconproject.org/)开发一个推送通知库,如果网络连接中断或更改,它需要采取行动 - 即,它需要重新启动服务器连接或暂停其操作,直到网络连接可用。对于“android.net.ConnectivityManager.CONNECTIVITY_ACTION”,这似乎可以很好地使用 Android BroadcastReceiver。
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.
我的问题是在测试库 - 我想在各种配置条件下自动测试库对断开的网络连接或从 3G 到 WiFi 的转换的响应。问题是,我不想整天坐在模拟器旁边按 F8。
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.
有没有办法在 jUnit 测试中以编程方式操作 Android 上的网络连接,而无需切换飞行模式?我已经尝试过通过控制台向模拟器发出命令、操纵 GSM 模式等,但是当显示屏上的电话状态发生变化时,互联网连接仍然存在。
回答by Igor
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".
在模态显示窗口的左侧选择“Android 应用程序”,在同一窗口的右侧选择“目标”选项卡。
At the bottom of the window, in the "Emulator Launch Parameters" section you have plethora of options regarding internet connectivity.
在窗口底部的“模拟器启动参数”部分,您有大量有关 Internet 连接的选项。
I hope this helped.
我希望这有帮助。
Igor
伊戈尔
回答by Sander Versluys
I've found the Android Dev Toolsapp really helpful.
我发现Android Dev Tools应用程序真的很有帮助。
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.
它允许您操作重要的系统设置,如网络连接。它甚至允许您设置自动打开和关闭 wifi 的时间间隔,从而允许您测试与网络相关的广播接收器。
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:
如果您想在真实的开发设备上安装 Dev Tools 应用程序,您可以从模拟器中复制该应用程序,然后使用 ADB 将其安装到您的设备上。要从正在运行的模拟器复制应用程序,请执行:
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:
这会将 .apk 文件复制到当前目录中。然后将其安装在您连接的设备上:
adb -d install Development.apk
回答by Jeff Axelrod
Unfortunately, there is no way to truly disable network access from within a unit test programmatically. I filed an Android enhancement requestfor this issue.
不幸的是,没有办法以编程方式从单元测试中真正禁用网络访问。我为此问题提交了Android 增强请求。
回答by Chris
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.
您可能还想查看 eclipse 中的模拟器控件视图。目前我没有太多运气让它杀死我的数据,但看起来应该如此。
回答by Snicolas
A simple, but a bit long and heavy, solution is too add an abstraction layer over your wifi connection state monitor.
一个简单但有点长且繁重的解决方案是在您的 wifi 连接状态监视器上添加一个抽象层。
Add an Entity (NetworkStateManager), it should an application scoped singleton (see RoboGuice or Dagger to this cleanly, or access the unique instance via your application instance).
添加一个实体 (NetworkStateManager),它应该是一个应用程序范围的单例(请参阅 RoboGuice 或 Dagger,或者通过您的应用程序实例访问唯一实例)。
Add methods to register, unregister listeners to this entity, using Observable Observer design pattern.
使用 Observable Observer 设计模式添加方法来注册、取消注册此实体的侦听器。
Each activity will have to register to it during onStart, unregister in onStop.
每个活动都必须在 onStart 期间注册,在 onStop 中取消注册。
The entity itself will be modified by a BroadCastReceiver.
实体本身将由 BroadCastReceiver 修改。
That is fully testable, if you use a DI framework, you will be able to inject a drivable stub into your activities and see how they react to network state changes.
这是完全可测试的,如果您使用 DI 框架,您将能够将一个可驱动的存根注入您的活动,并查看它们如何对网络状态变化做出反应。