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.
我正在尝试编写一个应用程序,需要知道何时没有可用的IP网络连接。我正在使用android.net.conn.CONNECTIVITY_CHANGE广播事件以及ConnectivityManager来响应状态的变化来实现这一点,但是我在模拟器中测试我的设置时遇到了问题。
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.
我尝试了两种飞行模式并按下F8以禁用“蜂窝网络”,但即使这两种情况都参与其中,应用程序仍“看到”底层网络。
Has anybody managed to find a way to simulate a total lack of network access?
有没有人设法找到一种模拟完全缺乏网络访问的方法?
5 个解决方案
#1
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
我已经体验过,在按下F8后,状态栏中的图标显示没有连接,但如果您尝试浏览它可以工作。也许它与这个被打开的bug有关:bug 3838
#2
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 :)
Eclipse中有DDMS Perspective,您可以使用连接速度和可用性进行操作(在“模拟器控制”选项卡中)。如果它不适合你,我可能会建议打开你的操作系统的网络,甚至插上电缆:)
#3
3
Oops, I meant to post this response (http://*.com/questions/3400109/simulate-wireless-network-in-emulator/6078544#6078544) here.
哎呀,我打算在这里发布这个回复(http://*.com/questions/3400109/simulate-wireless-network-in-emulator/6078544#6078544)。
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"
编写以下脚本并将其命名为“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.
当网络关闭设备时,您也将无法访问adb,但一旦重新建立连接,它将再次起作用。希望这可以帮助。
#4
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
isAvailable - 如果没有这个,当禁用WIFI时,isConnected可以返回TRUE。有关详细信息,请参阅下面的代码和此网址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:
将android.permission.ACCESS_NETWORK_STATE权限请求添加到应用程序清单:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
#5
1
I prefer the svc
command
我更喜欢svc命令
svc wifi disable
svc wifi enable
over the netcfg
command
通过netcfg命令
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.
因为当你使用后者关闭wifi时,它会在一段时间后恢复,我没有找到原因。
#1
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
我已经体验过,在按下F8后,状态栏中的图标显示没有连接,但如果您尝试浏览它可以工作。也许它与这个被打开的bug有关:bug 3838
#2
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 :)
Eclipse中有DDMS Perspective,您可以使用连接速度和可用性进行操作(在“模拟器控制”选项卡中)。如果它不适合你,我可能会建议打开你的操作系统的网络,甚至插上电缆:)
#3
3
Oops, I meant to post this response (http://*.com/questions/3400109/simulate-wireless-network-in-emulator/6078544#6078544) here.
哎呀,我打算在这里发布这个回复(http://*.com/questions/3400109/simulate-wireless-network-in-emulator/6078544#6078544)。
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"
编写以下脚本并将其命名为“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.
当网络关闭设备时,您也将无法访问adb,但一旦重新建立连接,它将再次起作用。希望这可以帮助。
#4
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
isAvailable - 如果没有这个,当禁用WIFI时,isConnected可以返回TRUE。有关详细信息,请参阅下面的代码和此网址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:
将android.permission.ACCESS_NETWORK_STATE权限请求添加到应用程序清单:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
#5
1
I prefer the svc
command
我更喜欢svc命令
svc wifi disable
svc wifi enable
over the netcfg
command
通过netcfg命令
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.
因为当你使用后者关闭wifi时,它会在一段时间后恢复,我没有找到原因。