如何判断Android网络连接情况?

发布于 2024-08-11 02:23:37 字数 52 浏览 5 评论 0原文

如何确定 Android 设备当前可用的互联网连接类型?例如让它返回 WiFi、3G、无。

How can I determine the current internet connection type available to an Android device? e.g. have it return WiFi, 3G, none.

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

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

发布评论

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

评论(4

如痴如狂 2024-08-18 02:23:37

您可以使用它来确定您是否已连接:

final ConnectivityManager connectManager = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE); // ctx stands for the Context
final NetworkInfo mobile = connectManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
final NetworkInfo wifi   = connectManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI  );

// Return true if connected, either in 3G or wi-fi
return ((mobile != null && mobile.getState() == NetworkInfo.State.CONNECTED) || 
        (wifi   != null && wifi.getState()   == NetworkInfo.State.CONNECTED)   );
}

此代码需要权限 ACCESS_NETWORK_STATEACCESS_WIFI_STATE

编辑public NetworkInfo getNetworkInfo(int networkType) 已在 API 21 中弃用。对于高于 20 的 API 级别,您可以执行以下操作:

final Network[] networks = connectManager.getAllNetworks();
boolean connected = false;

for (int ctr = 0; !connected && ctr < networks.length; ++ctr) {
    final NetworkInfo network = connectManager.getNetworkInfo(networks[ctr]);
    final int netType = network.getType();

    connected = (network.getState() == NetworkInfo.State.CONNECTED) &&
            (
                netType == ConnectivityManager.TYPE_MOBILE     ||
                netType == ConnectivityManager.TYPE_WIFI     /*||
                netType == ConnectivityManager.TYPE_WIMAX      ||
                netType == ConnectivityManager.TYPE_ETHERNET */  );
}

return connected;

我根据 OP 的问题将此代码限制为 Wi-Fi 和 3G,但很容易将其扩展到新添加的连接类型,例如以太网或 Wi-Max。

You can use this to determine whether you are connected:

final ConnectivityManager connectManager = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE); // ctx stands for the Context
final NetworkInfo mobile = connectManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
final NetworkInfo wifi   = connectManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI  );

// Return true if connected, either in 3G or wi-fi
return ((mobile != null && mobile.getState() == NetworkInfo.State.CONNECTED) || 
        (wifi   != null && wifi.getState()   == NetworkInfo.State.CONNECTED)   );
}

This code requires the permissions ACCESS_NETWORK_STATE and ACCESS_WIFI_STATE.

EDIT: public NetworkInfo getNetworkInfo (int networkType) has been deprecated in API 21. For API levels higher than 20 you can do as follows:

final Network[] networks = connectManager.getAllNetworks();
boolean connected = false;

for (int ctr = 0; !connected && ctr < networks.length; ++ctr) {
    final NetworkInfo network = connectManager.getNetworkInfo(networks[ctr]);
    final int netType = network.getType();

    connected = (network.getState() == NetworkInfo.State.CONNECTED) &&
            (
                netType == ConnectivityManager.TYPE_MOBILE     ||
                netType == ConnectivityManager.TYPE_WIFI     /*||
                netType == ConnectivityManager.TYPE_WIMAX      ||
                netType == ConnectivityManager.TYPE_ETHERNET */  );
}

return connected;

I limited this code to Wi-Fi and 3G as per the OP's question but it's easy to extend it to newly added connection types such as Ethernet or Wi-Max.

大姐,你呐 2024-08-18 02:23:37

不知道为什么您的应用程序崩溃,也许这有帮助。也许尝试这个方法:(需要应用程序上下文)

 public static boolean isInternetConnectionActive(Context context) {
  NetworkInfo networkInfo = ((ConnectivityManager) context
    .getSystemService(Context.CONNECTIVITY_SERVICE))
    .getActiveNetworkInfo();

  if(networkInfo == null || !networkInfo.isConnected()) {
   return false;
  }
  return true;
 }

然后尝试调用

if(isInternetConnectionActive(getApplicationContext)) {
 /* start Intent */
}
else {
  /* show Toast */
}

希望这会有所帮助

Don't know why your app crashes, maybe this helps. Maybe try this method: (needs the application context)

 public static boolean isInternetConnectionActive(Context context) {
  NetworkInfo networkInfo = ((ConnectivityManager) context
    .getSystemService(Context.CONNECTIVITY_SERVICE))
    .getActiveNetworkInfo();

  if(networkInfo == null || !networkInfo.isConnected()) {
   return false;
  }
  return true;
 }

then try to call

if(isInternetConnectionActive(getApplicationContext)) {
 /* start Intent */
}
else {
  /* show Toast */
}

hope this helps

蓝眼泪 2024-08-18 02:23:37

看到这个帖子,我回答了同样的问题
如何在 Android 应用程序中保存请求并重新发送< /a>

see this post, I answered the same question
how to save request and resend in android app

无悔心 2024-08-18 02:23:37

您还可以使用 TelephonyManager 来了解移动网络类型是:

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int networkType = telephonyManager.getNetworkType();

可能的网络类型列于 http:// developer.android.com/reference/android/telephony/TelephonyManager.html#getNetworkType%28%29

You can also use the TelephonyManager to find out what the mobile network type is:

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int networkType = telephonyManager.getNetworkType();

Possible network types are listed at http://developer.android.com/reference/android/telephony/TelephonyManager.html#getNetworkType%28%29

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