如何在Android模拟器中禁用/启用网络、切换到Wifi?

发布于 2024-09-05 01:19:20 字数 458 浏览 4 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甚是思念 2024-09-12 01:19:20

右键单击“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

清音悠歌 2024-09-12 01:19:20

我发现 Android 开发工具 应用程序非常有用。

它允许您操纵重要的系统设置,例如网络连接。它甚至允许您设置自动打开和关闭 wifi 的时间间隔,从而允许您测试与网络相关的广播接收器。

如果您想在真实的计算机上安装开发工具应用程序
开发设备,您可以从模拟器复制应用程序
然后使用 ADB 将其安装到您的设备上。复制申请表
从正在运行的模拟器中,执行:

adb -e pull /system/app/Development.apk ./Development.apk

这会将 .apk 文件复制到当前目录中。然后安装它
在您连接的设备上:

adb -d install Development.apk

在此处输入图像描述

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.

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:

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:

adb -d install Development.apk

enter image description here

难得心□动 2024-09-12 01:19:20

您可能还想查看 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.

alt text

一指流沙 2024-09-12 01:19:20

不幸的是,没有办法以编程方式真正从单元测试中禁用网络访问。我针对此问题提交了 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.

夏日浅笑〃 2024-09-12 01:19:20

一个简单但有点长和重的解决方案是在你的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文