如何在Android模拟器中禁用/启用网络、切换到Wifi?
我正在开发一个 Android 推送通知库 (http://deaconproject.org/),需要采取行动如果网络连接中断或发生变化,即需要重新启动服务器连接或暂停其操作,直到网络连接可用。使用“android.net.ConnectivityManager.CONNECTIVITY_ACTION”的 Android BroadcastReceiver 似乎可以正常工作。
我的问题是测试库 - 我想在各种配置条件下自动测试库对网络连接中断或从 3G 到 WiFi 的转换的响应。问题是,我不想整天坐在模拟器上按 F8。
有没有一种方法可以在 jUnit 测试中以编程方式操作 Android 上的网络连接,而无需切换飞行模式?我已经尝试通过控制台向模拟器发出命令、操作 GSM 模式等,但是当显示屏上的手机状态发生变化时,互联网连接仍然保持不变。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
右键单击“Project Explorer”面板中的项目,选择“Run As”,然后选择“Run Configurations”。
在模态显示窗口的左侧选择“Android 应用程序”,在同一窗口的右侧选择“目标”选项卡。
在窗口底部的“模拟器启动参数”部分中,您有大量有关互联网连接的选项。
我希望这有帮助。
伊戈尔
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
我发现 Android 开发工具 应用程序非常有用。
它允许您操纵重要的系统设置,例如网络连接。它甚至允许您设置自动打开和关闭 wifi 的时间间隔,从而允许您测试与网络相关的广播接收器。
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.
您可能还想查看 Eclipse 中的模拟器控制视图。目前,我没有太多运气让它杀死我的数据,但看起来应该。
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.
不幸的是,没有办法以编程方式真正从单元测试中禁用网络访问。我针对此问题提交了 Android 增强请求。
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.
一个简单但有点长和重的解决方案是在你的 wifi 连接状态监视器上添加一个抽象层。
添加一个实体(NetworkStateManager),它应该是一个应用程序范围的单例(请参阅 RoboGuice 或 Dagger,或者通过您的应用程序实例访问唯一实例)。
使用 Observable 观察者设计模式添加方法来注册、取消注册该实体的侦听器。
每个 Activity 都必须在 onStart 期间注册,在 onStop 期间取消注册。
实体本身将由 BroadCastReceiver 进行修改。
这是完全可测试的,如果您使用 DI 框架,您将能够将可驱动的存根注入到您的活动中,并查看它们对网络状态变化的反应。
A simple, but a bit long and heavy, solution is too add an abstraction layer over your wifi connection state monitor.
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).
Add methods to register, unregister listeners to this entity, using Observable Observer design pattern.
Each activity will have to register to it during onStart, unregister in onStop.
The entity itself will be modified by a 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.