27

I'm trying to write an application that needs to know when there is no IP network connection available. I am using the android.net.conn.CONNECTIVITY_CHANGE broadcast event along with ConnectivityManager to react to the changes in state to achieve this, but I'm having problems testing my set up in the emulator.

I have tried both flight mode and pressing F8 to disable the "Cellular Network" but even with both of these engaged the application still "sees" the underlying network.

Has anybody managed to find a way to simulate a total lack of network access?

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • @Hardlib: can u try the new updated answer posted by me? – Sankar Ganesh PMP Nov 11 '10 at 14:36
  • I think I've worked out what the problem I'm seeing is here. I have a 3rd party library that does some network work that continues to work in the emulator even after the sample code says the network is down. It is possible to tear down the 3rd party connection so I'm going to do this when the ConnectivityManager says there is no network. – hardillb Nov 11 '10 at 15:13

5 Answers5

4

I have experienced that after pressing F8 an icon in the status bar shows that there's no connectivity, but if you try to browse it works. Maybe it has something to do with this opened bug: bug 3838

Javi
  • 19,387
  • 30
  • 102
  • 135
  • Could put Windows 8 into airplane mode if your computer is wireless, that's a decent "real-life" way to test this as the emulator has no idea what's happened. – AutoM8R Aug 17 '13 at 02:41
4

There is DDMS Perspective in Eclipse, where you can manipulate with connection speed and availability (in the Emulator Control Tab). If it doesn't work for you, I may may suggest to turn on network of your OS or even plug off cable :)

Anton Derevyanko
  • 3,405
  • 1
  • 24
  • 32
3

Oops, I meant to post this response (http://stackoverflow.com/questions/3400109/simulate-wireless-network-in-emulator/6078544#6078544) here.

Here is the solution I came up with for simulating total network connection loss on the emulator:

Write the following script and name it "nonetwork.sh"

netcfg eth0 down
sleep 10
netcfg eth0 up
netcfg eth0 dhcp

Upload the following script to the emulator via this command:

adb push nonetwork.sh /data/local/nonetwork.sh

Change the permissions

adb shell chmod 700 /data/local/nonetwork.sh

Run it

adb shell /data/local/nonetwork.sh

While the network is down on the device you'll lose access to adb as well but once the connection is re-established it will work again. Hope this helps.

Bellinghammer
  • 185
  • 3
  • 9
  • Great tip, tnx! Only small note: users of this tip must preserve script location. Scripts under /mnt have "spoiled owner". – Grzegorz Dev Dec 04 '13 at 14:42
1

isAvailable - without this isConnected can return TRUE when WIFI is disabled. Refer to the code below and this url for more information http://developer.android.com/guide/developing/tools/emulator.html

    ConnectivityManager conMgr = ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

      if (
connMgr.getActiveNetworkInfo() != null &&
            conMgr.getActiveNetworkInfo().isAvailable() &&

    conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED || conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING ) {

          //notify user you are online

      }       else if (

    conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) { //notify user you are not online

          Toast.makeText(getBaseContext(),"Please

    Check Your Internet Connection and Try Again",Toast.LENGTH_SHORT).show();

      }

Add the android.permission.ACCESS_NETWORK_STATE permission request to your application manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Basic
  • 26,321
  • 24
  • 115
  • 201
Sankar Ganesh PMP
  • 11,927
  • 11
  • 57
  • 90
  • Hi, I have some very similar code to this, the problem is that it then triggers some complex code that I need to test and debug. It is the testing these events that is the problem, which is why I want to simulate the triggers that will activate the code. – hardillb Nov 11 '10 at 13:51
  • @hardlib: can u elaborate this, I am getting your message clear? – Sankar Ganesh PMP Nov 11 '10 at 13:58
1

I prefer the svc command

svc wifi disable
svc wifi enable

over the netcfg command

netcfg mlan0 up/down

Because when you turn down wifi using the latter, it will recover after a while which I haven't found out why.

Echilon
  • 10,064
  • 33
  • 131
  • 217
Nick X
  • 176
  • 1
  • 7