如何检查networking连接启用或禁用在移动WIFI和3G(数据计划)?

我正在开发一个Android应用程序,在我的应用程序,我想检查networking连接,就像我想检查在WiFi和3Gnetworking连接(如印度人主要是像移动数据计划),如何检查networking在WiFi和3G.naybody知道,请给一些想法。

谢谢

Solutions Collecting From Web of "如何检查networking连接启用或禁用在移动WIFI和3G(数据计划)?"

这是代码片段。 如果networking启用,则返回true,否则返回false

private boolean netCheckin() { try { ConnectivityManager nInfo = (ConnectivityManager) getSystemService( Context.CONNECTIVITY_SERVICE); nInfo.getActiveNetworkInfo().isConnectedOrConnecting(); Log.d(tag, "Net avail:" + nInfo.getActiveNetworkInfo().isConnectedOrConnecting()); ConnectivityManager cm = (ConnectivityManager) getSystemService( Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnectedOrConnecting()) { Log.d(tag, "Network available:true"); return true; } else { Log.d(tag, "Network available:false"); return false; } } catch (Exception e) { return false; } } 

请试试这个

 public static boolean isInternetConnected (Context ctx) { ConnectivityManager connectivityMgr = (ConnectivityManager) ctx .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo wifi = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo mobile = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); // Check if wifi or mobile network is available or not. If any of them is // available or connected then it will return true, otherwise false; if (wifi != null) { if (wifi.isConnected()) { return true; } } if (mobile != null) { if (mobile.isConnected()) { return true; } } return false; } 

请在Android清单文件中添加以下权限。

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

你可以使用下面的代码,这是适用于所有的API版本:

 ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); if(isConnected) { if(activeNetwork.getType()==ConnectivityManager.TYPE_MOBILE) return true; else return false; } else return false; 

我在Activity类中使用这个

 private boolean isConnectedToInternet() { ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); return (networkInfo != null); }