Android的互联网连接超时
问题描述:
我能够检测使用Android的互联网连接超时
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,但没有互联网连接互联网。在这种情况下,如果互联网不可用,我会在10秒后向用户显示一条消息[无INTERNET CHECK YOUR CONNECTION]
我该如何实现它。
答
您可以通过时间完成的消息将显示给用户
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// This method will be executed once the timer is over
// Your message here
}
}, 10000); //Your time here in miliseconds
后使用处理器实现这一目标。简单的是。
保持10秒,你可以添加10000
答
您可以尝试以下操作来检查网络连接和therefafter显示敬酒的情况下,没有互联网:
boolean isInternetAvailable = checkInternetConnection(this);
if(!isInternetAvailable) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(this, "No INTERNET CHECK YOUR CONNECTION", Toast.LENGTH_SHORT).show();
}
}, 10000);
}
public static boolean checkInternetConnection(Context context) {
final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();
if (activeNetworkInfo != null) { // connected to the internet
Toast.makeText(context, activeNetworkInfo.getTypeName(), Toast.LENGTH_SHORT).show();
if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
return isWifiInternetAvailable();
} else if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
// connected to the mobile provider's data plan
return true;
}
}
return false;
}
public boolean isWifiInternetAvailable() {
try {
InetAddress ipAddr = InetAddress.getByName("google.com"); //You can replace it with your name
return !ipAddr.equals("");
} catch (Exception e) {
return false;
}
}
答
你是对的,你可以检查您是否连接到wifi
或Cellular Network
,但您无法轻易检查您是否真的连接到Internet
。
现在检查您是否真正连接到互联网的唯一方法是连接到远程服务器!例如:
public static boolean hasInternetAccess(Context context) {
if (isNetworkAvailable(context)) {
try {
HttpURLConnection urlc = (HttpURLConnection)
(new URL("http://clients3.google.com/generate_204")
.openConnection());
urlc.setRequestProperty("User-Agent", "Android");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 204 &&
urlc.getContentLength() == 0);
} catch (IOException e) {
Log.e(TAG, "Error checking internet connection", e);
}
} else {
Log.d(TAG, "No network available!");
}
return false;
}
您可以更改URL到任何需要的地址,但这个URL生成确定的响应,所以你不必担心服务器的问题。当然,你可以把它改成你的服务器查询服务器太^^
欲了解更多信息,你可以检查这个答案: Detect if Android device has Internet connection
感谢您的意见,让我试试这个。 – Andi
@ChandraMouliPoreddy肯定好友,如果它工作,那么不要忘记接受并评价答案。 :) –