How to disable/enable network, switch to Wifi in A

2019-01-14 01:15发布

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.

5条回答
该账号已被封号
2楼-- · 2019-01-14 01:48

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

查看更多
smile是对你的礼貌
3楼-- · 2019-01-14 01:50

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.

查看更多
趁早两清
4楼-- · 2019-01-14 02:06

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

查看更多
别忘想泡老子
5楼-- · 2019-01-14 02:10

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

查看更多
The star\"
6楼-- · 2019-01-14 02:12

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.

查看更多
登录 后发表回答