当wifi连接到无线调制解调器互联网覆盖范围是否存在时,它总是说是的,你连接它只检查wifi连接而不是互联网连接,那么如何处理这种情况?
问问题
6076 次
7 回答
3
1.)你可以检查它
URL url = new URL("YOUR urlString");
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
.
.
int responseCode = conn.getResponseCode();
//if responseCode = 200 - THEn CONN is connected
或者
2.) 你可以做一些像 dis
public static boolean isNetworkAvailable(Activity activity) {
ConnectivityManager connectivity = (ConnectivityManager) activity
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true; //<-- -- -- Connected
}
}
}
}
return false; //<-- -- -- NOT Connected
}
于 2012-05-30T13:26:13.653 回答
1
尝试这个。
private boolean haveNetworkConnection(Context context)
{
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo)
{
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
{
if (ni.isConnected())
{
haveConnectedWifi = true;
Log.v("WIFI CONNECTION ", "AVAILABLE");
} else
{
Log.v("WIFI CONNECTION ", "NOT AVAILABLE");
}
}
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
{
if (ni.isConnected())
{
haveConnectedMobile = true;
Log.v("MOBILE INTERNET CONNECTION ", "AVAILABLE");
} else
{
Log.v("MOBILE INTERNET CONNECTION ", "NOT AVAILABLE");
}
}
}
return haveConnectedWifi || haveConnectedMobile;
}
希望它会有所帮助。
于 2012-05-30T14:05:01.037 回答
1
使用此代码
final ConnectivityManager connectivityManager = (ConnectivityManager)context.
getSystemService(Context.CONNECTIVITY_SERVICE);
final Network network = connectivityManager.getActiveNetwork();
final NetworkCapabilities capabilities = connectivityManager
.getNetworkCapabilities(network);
return capabilities != null
&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
于 2016-07-25T11:21:16.403 回答
0
对,如果您使用 ConnectivityManager,则仅提供连接信息(WiFi、移动设备、WiMax 等)以及是否已连接。
为了确保数据连接,您可以进行 ping:
public static boolean ping() {
try {
SocketAddress addr = new InetSocketAddress("www.example.com", 80); // Set IP/Host and Port
Socket socket = new Socket();
//Connect socket to address, and set a time-out to 5 sec
socket.connect(addr, 5000);
//If network isn't conecctet then throw a IOException else socket is connected successfully
socket.close();
return true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
于 2012-05-30T13:42:19.800 回答
0
你试过这个吗?
http://developer.android.com/reference/android/net/NetworkInfo.html#isConnected ()
描述说:
指示是否存在网络连接,是否可以建立连接并传递数据。
于 2012-05-30T13:29:42.030 回答
0
您可以使用它来检查 Android 中的 Internet 连接是否有 wifi 或移动数据连接
public boolean isConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean NisConnected = activeNetwork != null && activeNetwork.isConnected();
if (NisConnected) {
// if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE || activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
return true;
else
return false;
}
return false;
}
于 2015-06-07T05:11:41.670 回答
0
也就是这样
public static boolean isNetworkAvailable(Context context)
{
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnectedOrConnecting();
}
于 2017-02-22T13:36:57.070 回答