我能够使用检测互联网连接
public class InternetDetector {
private Context _context;
public InternetDetector(Context context) {
this._context = context;
}
/**
* Checking for all possible internet providers
**/
public Boolean isConnectingToInternet() {
ConnectivityManager connectivityManager
= (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}
public Boolean isOnline() {
try {
Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
int returnVal = p1.waitFor();
boolean reachable = (returnVal == 0);
return reachable;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}
如果有互联网,这非常有效,但在某些情况下,我可以连接到 WIFI 但没有互联网。在这种情况下,如果 Internet 不可用,我需要在 10 秒后向用户显示消息 [No INTERNET CHECK YOUR CONNECTION]
我怎样才能实现它。