即使连接到 wifi 或 3G,也要检查互联网连接
Check internet connection even if connected to wifi or 3G
即使 phone 连接到 wifi 网络或 3G,我也需要检查是否有互联网连接。我有这段代码,但它仅在连接到网络时显示:
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
}
您可以使用以下方法检测是否连接了 3G 或 WiFi
ConnectivityManager man = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
//3G
boolean is3gConnected = man.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
//Wifi
boolean isWifiConnected = man.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
我创建这个 class 是为了了解用户是否连接到 Internet 以及他的连接类型
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
public class NetWork {
private static boolean connectionEstablished = false;
/**
* get the internet connection of the device
*
* @param context
* @return the type of internet connection
*/
private static NetWorkTypes getNetwork(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info == null || !info.isConnected())
return NetWorkTypes.NO_CONNECTION;
if (info.getType() == ConnectivityManager.TYPE_WIFI)
return NetWorkTypes.WIFI;
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
int networkType = info.getSubtype();
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: // all above for 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: // all above for 3G
case TelephonyManager.NETWORK_TYPE_LTE: // 4G
return NetWorkTypes.MOBILE_NETWORK;
default:
return NetWorkTypes.UNKNOWN;
}
}
return NetWorkTypes.UNKNOWN;
}
/**
* check if device is connected on the internet
*
* @param context
* @return true if is connected, false if isn't
*/
public static boolean isConnected(Context context) {
NetWorkTypes netWorkType;
netWorkType = getNetwork(context);
if (netWorkType == NetWorkTypes.WIFI || netWorkType == NetWorkTypes.MOBILE_NETWORK) {
connectionEstablished = true;
} else {
connectionEstablished = false;
}
return connectionEstablished;
}
/**
* @return the boolean that tells if there is connection on the internet
*/
public static boolean getConnectionEstablished() {
return connectionEstablished;
}
}
NetWorkTypes
只是一个枚举。您可以return任何您想要的
你说 即使连接到 wifi 或 3G 也要检查 *internet* 连接,但是你的实现实际上不会测试这个,因为仅仅连接到 WiFi 网络并不意味着您有 互联网 连接。作为建议,您可以尝试打开到已知主机的套接字 - 如果连接成功,您可以确定您可以访问网络。
public boolean hasInternetConnectivity() {
try {
// connect to google on port 80, the HTTP port
Socket s = new Socket("www.google.com", 80);
// the above would have thrown if failed, so we are good
s.close();
return true;
} catch (Exception e) {
// check logcat to see why it failed, you could then catch and handle each exception independently ( time out, host unknown, end of stream, etc.. )
e.printStackTrace();
// the connection has failed, return false
return false;
}
即使 phone 连接到 wifi 网络或 3G,我也需要检查是否有互联网连接。我有这段代码,但它仅在连接到网络时显示:
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
}
您可以使用以下方法检测是否连接了 3G 或 WiFi
ConnectivityManager man = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
//3G
boolean is3gConnected = man.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
//Wifi
boolean isWifiConnected = man.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
我创建这个 class 是为了了解用户是否连接到 Internet 以及他的连接类型
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
public class NetWork {
private static boolean connectionEstablished = false;
/**
* get the internet connection of the device
*
* @param context
* @return the type of internet connection
*/
private static NetWorkTypes getNetwork(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info == null || !info.isConnected())
return NetWorkTypes.NO_CONNECTION;
if (info.getType() == ConnectivityManager.TYPE_WIFI)
return NetWorkTypes.WIFI;
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
int networkType = info.getSubtype();
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: // all above for 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: // all above for 3G
case TelephonyManager.NETWORK_TYPE_LTE: // 4G
return NetWorkTypes.MOBILE_NETWORK;
default:
return NetWorkTypes.UNKNOWN;
}
}
return NetWorkTypes.UNKNOWN;
}
/**
* check if device is connected on the internet
*
* @param context
* @return true if is connected, false if isn't
*/
public static boolean isConnected(Context context) {
NetWorkTypes netWorkType;
netWorkType = getNetwork(context);
if (netWorkType == NetWorkTypes.WIFI || netWorkType == NetWorkTypes.MOBILE_NETWORK) {
connectionEstablished = true;
} else {
connectionEstablished = false;
}
return connectionEstablished;
}
/**
* @return the boolean that tells if there is connection on the internet
*/
public static boolean getConnectionEstablished() {
return connectionEstablished;
}
}
NetWorkTypes
只是一个枚举。您可以return任何您想要的
你说 即使连接到 wifi 或 3G 也要检查 *internet* 连接,但是你的实现实际上不会测试这个,因为仅仅连接到 WiFi 网络并不意味着您有 互联网 连接。作为建议,您可以尝试打开到已知主机的套接字 - 如果连接成功,您可以确定您可以访问网络。
public boolean hasInternetConnectivity() {
try {
// connect to google on port 80, the HTTP port
Socket s = new Socket("www.google.com", 80);
// the above would have thrown if failed, so we are good
s.close();
return true;
} catch (Exception e) {
// check logcat to see why it failed, you could then catch and handle each exception independently ( time out, host unknown, end of stream, etc.. )
e.printStackTrace();
// the connection has failed, return false
return false;
}