How to simulate total network loss in Android Emulator

淺唱寂寞╮ 提交于 2019-12-03 04:27:27

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

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 :)

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.

Sankar Ganesh

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" />
Nick X

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!