互联网连接Android模拟器始终显示连接状态互联网、模拟器、状态、Android

2023-09-06 02:02:36 作者:很帅不给撩

喜 我试过很多方法可以检查互联网连接在我的Andr​​oid模拟器

hi I tried in many ways to check Internet connection in my android emulator

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

   NetworkInfo info= conMgr.getActiveNetworkInfo();  

         if(info != null || info.isConnected()) {  
              Log.v("NetworkInfo","Connected State");  
         }  
        else{  
            Log.v("NetworkInfo","Not Connected state");  
            Log.v("Reason",info.getReason());  
        } 

即使我禁用Internet连接在我的系统,我的code似乎显示连接状态 所以我猜这可能工作

even if i disable Internet connection in my system ,my code seems to display Connected state so i guessed this may work

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

      Log.v("Congr","Connection Present");

    }
    else if ( conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED
        ||  conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED) {

      Log.v("conMgr"," No Connection");

    } 

但上面的code还会显示连接present即使我断开网络电缆。 。请帮忙找做到这一点。我以正确的方式有ACCESS_NETWORK_STATE和INTERNET权限在我的清单文件

but the above code also displays "Connection Present" even when i disconnect Internet cables. .Please help to find a correct way to do this .I have ACCESS_NETWORK_STATE and INTERNET permission in my manifest file

推荐答案

1 的断开网络电缆是无关紧要的。使用F8在模拟器关闭/开启网络

1 Disconnecting internet cables is irrelevant. Use F8 to switch off/on internet in the emulator

2 的在C这个逻辑的第一个$ C $是错误的:

2 In the first code this logic is wrong:

if(info != null || info.isConnected()) {  
              Log.v("NetworkInfo","Connected State");  
         } 

这应该是:

if(info != null && info.isConnected()) {  
              Log.v("NetworkInfo","Connected State");  
         } 

如果你用||然后这是结果: 一)当信息为空,info.isConnected()会崩溃 B)当信息不为空,你将总是显示连接状态

If you use || then this is the result: a) when info is null, info.isConnected() will crash b) when info is not null, you will always show Connected State

不过,即使你解决这个问题,在code不会是完全正确的(见 4 的)

However, even if you fix that, the code wont be completely correct (see 4)

3 的第二code,这种逻辑是错误的:

3 In the second code, this logic is wrong:

 else if ( conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED
        ||  conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED) {

      Log.v("conMgr"," No Connection");

    } 

应该是:

 else {

      Log.v("conMgr"," No Connection");

    } 

为什么呢?因为有更多的国家比连接和断开。

Why? Because there are more states than CONNECTED and DISCONNECTED.

不过,即使你解决这个问题,在code不会是完全正确的(见 4 的)

However, even if you fix that, the code wont be completely correct (see 4)

4 的这个工程的仿真器和设备。

4 This works on emulator and devices.

connected = (   conMgr.getActiveNetworkInfo() != null &&
            conMgr.getActiveNetworkInfo().isAvailable() &&
            conMgr.getActiveNetworkInfo().isConnected()   )

请注意使用isAvailable的 - 没有这个isConnected可以返回TRUE WIFI时禁用

Note the use of isAvailable - without this isConnected can return TRUE when WIFI is disabled.

希望这有助于!