如何确定的Andr​​oid上网吗? [英] How to determine Android internet connection?

查看:83
本文介绍了如何确定的Andr​​oid上网吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能够确定当前可用的网络连接类型的Andr​​oid设备?例如有它返回的WiFi,3G,没有。

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

推荐答案

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

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

这code要求的权限 ACCESS_NETWORK_STATE ACCESS_WIFI_STATE

This code requires the permissions ACCESS_NETWORK_STATE and ACCESS_WIFI_STATE.

这篇关于如何确定的Andr​​oid上网吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆