如何检查WiFi或3Gnetworking在Android设备上可用
在这里,我的Android设备同时支持WiFi和3G。 在特定时间哪个networking在这个设备上可用。 因为我的要求是3G时,我必须上传less量的数据。 当wifi可用时,整个数据必须上传。 所以,我要检查连接是WiFi还是3G。 请帮帮我。 提前致谢。
我使用这个:
/** * Checks if we have a valid Internet Connection on the device. * @param ctx * @return True if device has internet * * Code from: http://www.androidsnippets.org/snippets/131/ */ public static boolean haveInternet(Context ctx) { NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); if (info == null || !info.isConnected()) { return false; } if (info.isRoaming()) { // here is the roaming option you can change it if you want to // disable internet while roaming, just return false return false; } return true; }
你也需要
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
在AndroidMainfest.xml中
要获得networkingtypes,您可以使用以下代码片段:
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); //mobile State mobile = conMan.getNetworkInfo(0).getState(); //wifi State wifi = conMan.getNetworkInfo(1).getState();
然后像这样使用它:
if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) { //mobile } else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) { //wifi }
要获得移动networking的types,我会尝试TelephonyManager#getNetworkType或NetworkInfo#getSubtypeName
你需要在Android清单文件中添加下面的权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
之后,你可以使用下面的function来检查WiFi或移动networking连接与否
public static boolean isWifiConnected(Context context) { ConnectivityManager connManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); return ((netInfo != null) && netInfo.isConnected()); } public static boolean isMobileConnected(Context context) { ConnectivityManager connManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); return ((netInfo != null) && netInfo.isConnected()); }
来自developer.android.com的一些引用是:
首先得到ConnectivityManager的参考,然后检查设备的Wifi和3G状态 。 您需要ACCESS_NETWORK_STATE权限才能使用此服务。
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mWifi.isAvailable() == true) { return "Connected to WiFi"; } else if (mMobile.isAvailable() == true) { return "Connected to Mobile Network"; } else return "No internet Connection"
您可以使用此方法检查您的互联网连接是2G,3G还是4G :
public String getNetworkClass(Context context) { TelephonyManager mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); int networkType = mTelephonyManager.getNetworkType(); switch (networkType) { case TelephonyManager.NETWORK_TYPE_GPRS: case TelephonyManager.NETWORK_TYPE_EDGE: case TelephonyManager.NETWORK_TYPE_CDMA: case TelephonyManager.NETWORK_TYPE_1xRTT: case TelephonyManager.NETWORK_TYPE_IDEN: return "2G"; case TelephonyManager.NETWORK_TYPE_UMTS: case TelephonyManager.NETWORK_TYPE_EVDO_0: case TelephonyManager.NETWORK_TYPE_EVDO_A: case TelephonyManager.NETWORK_TYPE_HSDPA: case TelephonyManager.NETWORK_TYPE_HSUPA: case TelephonyManager.NETWORK_TYPE_HSPA: case TelephonyManager.NETWORK_TYPE_EVDO_B: case TelephonyManager.NETWORK_TYPE_EHRPD: case TelephonyManager.NETWORK_TYPE_HSPAP: return "3G"; case TelephonyManager.NETWORK_TYPE_LTE: return "4G"; default: return "Unknown"; } }
并使用以下方法,您可以检查互联网是否可用 ,并确定您是否通过移动networking或WiFi访问互联网:
public String getNetworkType(Context context){ String networkType = null; ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo(); if (activeNetwork != null) { // connected to the internet if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) { networkType = "WiFi"; } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) { networkType = "Mobile"; } } else { // not connected to the internet } return networkType; }