当wifi连接到无线调制解调器互联网覆盖是否存在它总是说是你被连接它只检查wifi连接而不是互联网连接所以如何处理这种情况?
答案 0 :(得分:3)
1.)你可以把它检查为
URL url = new URL("YOUR urlString");
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
.
.
int responseCode = conn.getResponseCode();
//if responseCode = 200 - THEn CONN is connected
OR
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
}
答案 1 :(得分:2)
试试这个。
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;
}
希望它会有所帮助。
答案 2 :(得分:1)
使用此代码 最终ConnectivityManager connectivityManager =(ConnectivityManager)上下文。 getSystemService(Context.CONNECTIVITY_SERVICE);
final Network network = connectivityManager.getActiveNetwork();
final NetworkCapabilities capabilities = connectivityManager
.getNetworkCapabilities(network);
return capabilities != null
&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
答案 3 :(得分:0)
你试过这个吗?
http://developer.android.com/reference/android/net/NetworkInfo.html#isConnected()
描述说:
指示是否存在网络连接,并且可以建立连接并传递数据。
答案 4 :(得分: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;
}
答案 5 :(得分: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;
}
答案 6 :(得分:0)
也就是这样
public static boolean isNetworkAvailable(Context context)
{
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnectedOrConnecting();
}