Android 互联网连接检查问题

发布于 2024-08-31 08:57:43 字数 723 浏览 1 评论 0原文

我是 Android 开发新手,正在开发一个 Android 应用程序,该应用程序需要手机通过 Wifi、EDGE 或 3G 连接到互联网。

这是我用来检查互联网连接是否可用的代码

public static boolean isConnected()
{
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    return cm.getActiveNetworkInfo().isConnectedOrConnecting();
}

我还在清单文件中设置了这些权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

当启用 3G 时,这在运行 Android 1.5 版本的模拟器中运行良好,但当我禁用时它会崩溃3G 连接。当我调用 isConnectedOrConnecting() 时,我的应用程序抛出空指针异常。我的运行 Android 2.1 的 HTC Desire 也会发生同样的情况。

希望有人知道这个问题的解决方案。

提前致谢!

I'm new to Android development and working on an Android application that requires the phone to be connected to the internet, through either Wifi, EDGE or 3G.

This is the code that I'm using to check whether an internet connection is available

public static boolean isConnected()
{
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    return cm.getActiveNetworkInfo().isConnectedOrConnecting();
}

I've also set these permissions in the manifest file

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

This works fine in the emulator running version 1.5 of Android when 3G is enabled, but it crashes when I disable the 3G connection. My application throws a null pointer exception when I call isConnectedOrConnecting(). The same thing also happens on my HTC Desire running Android 2.1.

Hope that anyone knows the solution to this.

Thanks in advance!

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

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

发布评论

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

评论(5

庆幸我还是我 2024-09-07 08:57:43

如果崩溃直接发生在您的线路上:

return cm.getActiveNetworkInfo().isConnectedOrConnecting();

那么这意味着 getActiveNetworkInfo() 返回 null,因为没有活动网络 - 在这种情况下,您的 isConnected() 方法应返回 false

If the crash is directly on your line:

return cm.getActiveNetworkInfo().isConnectedOrConnecting();

then that means getActiveNetworkInfo() returned null, because there is no active network -- in that case, your isConnected() method should return false.

梅窗月明清似水 2024-09-07 08:57:43

我写了这个方法来处理这个问题:

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if (ni!=null && ni.isAvailable() && ni.isConnected()) {
        return true;
    } else {
        return false; 
    }
}

我想这是一种方法......

I wrote this method to handle this:

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if (ni!=null && ni.isAvailable() && ni.isConnected()) {
        return true;
    } else {
        return false; 
    }
}

One way to do it I guess...

薄荷梦 2024-09-07 08:57:43

要检查互联网是否存在,只能在设备上检查......在模拟器上它可能不起作用......我有以下代码&它在 Android 设备上 100% 工作......:)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tv = (TextView)findViewById(R.id.txt);
    b = checkInternetConnection();


    if(b!=true)
    {
        tv.setText("net is not dr.......");
    }
    else
    {
        tv.setText("net is dr.......");
    }

}
//Check weather Internet connection is available or not
public boolean checkInternetConnection() {
           final ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);
           if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() &&    conMgr.getActiveNetworkInfo().isConnected()) {
                 return true;
           } else {
                 System.out.println("Internet Connection Not Present");
               return false;
           }
        }

}

To check internet is there or not can be checked only on device......On emulator it may not work.... I have got the following code & its working 100% on android device..... :)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tv = (TextView)findViewById(R.id.txt);
    b = checkInternetConnection();


    if(b!=true)
    {
        tv.setText("net is not dr.......");
    }
    else
    {
        tv.setText("net is dr.......");
    }

}
//Check weather Internet connection is available or not
public boolean checkInternetConnection() {
           final ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);
           if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() &&    conMgr.getActiveNetworkInfo().isConnected()) {
                 return true;
           } else {
                 System.out.println("Internet Connection Not Present");
               return false;
           }
        }

}

梦里兽 2024-09-07 08:57:43

您已经使用了这个片段。

ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity != null)
      {
          NetworkInfo[] info = connectivity.getAllNetworkInfo();
          if (info != null)
              for (int i = 0; i < info.length; i++)
                  if (info[i].getState() == NetworkInfo.State.CONNECTED)
                  {
                      return true;
                  }

      }

You have used this snippet.

ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity != null)
      {
          NetworkInfo[] info = connectivity.getAllNetworkInfo();
          if (info != null)
              for (int i = 0; i < info.length; i++)
                  if (info[i].getState() == NetworkInfo.State.CONNECTED)
                  {
                      return true;
                  }

      }
小女人ら 2024-09-07 08:57:43

使用它来确定是否连接到 wifi/3g:

is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
    isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
    network = is3g||isWifi;

并使用它自己启用 wifi:

WifiManager wifiManager = (WifiManager) MainWindowYuval.this.getSystemService(Context.WIFI_SERVICE);
                    wifiManager.setWifiEnabled(true);      

use this to detemine if connceted to wifi/3g:

is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
    isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
    network = is3g||isWifi;

and this to enable wifi yourself:

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