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

发布于 2024-08-28 02:48:23 字数 1078 浏览 5 评论 0原文

我尝试了多种方法来检查我的 Android 模拟器中的互联网连接,

  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());  
        } 

即使我在系统中禁用互联网连接,我的代码似乎显示已连接状态 所以我猜这可能有效,

 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");

    } 

但即使我断开互联网电缆,上面的代码也会显示“连接存在”。 。请帮助找到正确的方法来执行此操作。我的清单文件中有 ACCESS_NETWORK_STATE 和 INTERNET 权限

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());  
        } 

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");

    } 

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情释 2024-09-04 02:48:23

1 断开互联网电缆无关紧要。使用 F8 在模拟器中关闭/打开互联网

2 在第一个代码中,此逻辑是错误的:

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

它应该是:

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

如果您使用 ||那么结果是这样的:
a) 当info为null时,info.isConnected()会崩溃
b) 当 info 不为 null 时,您将始终显示 Connected State

但是,即使您修复了该问题,代码也不会完全正确(参见 4

3 在第二个中代码中,这个逻辑是错误的:

 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");

    } 

为什么?因为状态比 CONNECTED 和 DISCONNECTED 还多。

但是,即使您修复了该问题,代码也不会完全正确(请参阅4

4 这适用于模拟器和设备。

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

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

希望这有帮助!

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

2 In the first code this logic is wrong:

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

It should be:

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

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

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

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");

    } 

should be:

 else {

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

    } 

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

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

4 This works on emulator and devices.

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

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

Hope this helps!

送君千里 2024-09-04 02:48:23

实际上,这并不能告诉您模拟器是否通过主机 PC 连接到网络。如果您在 PC 上运行它并且 PC 的无线已关闭,只要模拟器认为其处于在线状态,它仍然会返回 true。我能够确定模拟器是否真正在线的唯一方法是尝试获取页面并查看它是否成功。

Actually, this doesn't tell you if the emulator is connected to the web via the hosting PC. If you run it on a PC and the PC's wireless is turned off it still returns true as long the emulator thinks its in online state. The only way I've been able to determine for sure whether the emulator is really online is to try to fetch a page and see if it succeeds or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文