How to simulate total network loss in Android Emulator

 ̄綄美尐妖づ 提交于 2019-12-03 14:41:13

问题


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?


回答1:


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




回答2:


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




回答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.




回答4:


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



回答5:


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.



来源:https://stackoverflow.com/questions/4154815/how-to-simulate-total-network-loss-in-android-emulator

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