我使用过以下代码For Wheather Intenet连接是否可用,它可以很好地工作到3G系统。但它不适用于4G技术。任何人都知道如何在4g上找到可用的互联网连接? 如果任何机构有民主代码,请提供
代码:
public static boolean checkConnection(Context c)
{
ConnectivityManager mConnectivityManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager telephonyManager = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
if(mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected() || telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED)
return true;
else
return false;
}
先谢谢 Nirav modh
答案 0 :(得分:1)
使用TYPE_WIMAX
但它在API级别8
http://developer.android.com/reference/android/net/ConnectivityManager.html#TYPE_WIMAX
答案 1 :(得分:0)
Context context = MainActivity.this;
public synchronized static boolean isNetAvailable(Context context){
boolean isNetAvailable=false;
if ( context != null ){
ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if ( mgr != null ){
boolean mobileNetwork = false;
boolean wifiNetwork = false;
boolean wiMaxNetwork = false;
boolean mobileNetworkConnecetd = false;
boolean wifiNetworkConnecetd = false;
boolean wiMaxNetworkConnected = false;
NetworkInfo mobileInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo wifiInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo wiMaxInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIMAX);
if ( mobileInfo != null )
mobileNetwork = mobileInfo.isAvailable();
if ( wifiInfo != null )
wifiNetwork = wifiInfo.isAvailable();
if(wiMaxInfo != null)
wiMaxNetwork = wiMaxInfo.isAvailable();
if(wifiNetwork == true || mobileNetwork == true || wiMaxNetwork == true){
mobileNetworkConnecetd = mobileInfo.isConnectedOrConnecting();
wifiNetworkConnecetd = wifiInfo.isConnectedOrConnecting();
wiMaxNetworkConnected = wiMaxInfo.isConnectedOrConnecting();
}
isNetAvailable = ( mobileNetworkConnecetd || wifiNetworkConnecetd || wiMaxNetworkConnected );
}
}
return isNetAvailable;
}
检查isNetAvailable的值在所有3个案例中都适用于我,也希望为你工作
注意:4G可用性将引入API级别8以后。
答案 2 :(得分:0)
检查所有互联网连接的最佳方法是这么简短和点代码:
public boolean isConnectingToInternet() {
ConnectivityManager connectivity = (ConnectivityManager) applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] allNetworkInfo = connectivity.getAllNetworkInfo();
if (allNetworkInfo != null)
for (NetworkInfo info : allNetworkInfo)
if (info.isConnectedOrConnecting()) {
return true;
}
}
return false;
}
答案 3 :(得分:0)
试试这个:
public static Boolean check_connection(final Context _context)
{
boolean connected;
ConnectivityManager conectivtyManager = (ConnectivityManager) _context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conectivtyManager.getActiveNetworkInfo() != null
&& conectivtyManager.getActiveNetworkInfo().isAvailable()
&& conectivtyManager.getActiveNetworkInfo().isConnected())
{
connected = true;
} else
{
connected = false;
}
return connected;
}
答案 4 :(得分:0)
public static boolean checkConnection(Context c)
{
ConnectivityManager connectivityManager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo!=null&&activeNetInfo.isConnected()){
Log.e(TAG, "Net connected");
return true;
}else{
Log.e(TAG, "Net Disconnected" );
return false;
}
}