[英]Android Internet Connection timeout
我可以使用檢測到Internet連接
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秒鍾后向用戶顯示一條消息[No 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
您可以嘗試以下方法檢查互聯網連接,然后在沒有互聯網的情況下顯示祝酒詞:
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會生成確定的響應,因此您不必擔心服務器問題。.當然,您也可以將其更改為服務器以檢查服務器的可用性^^
有關更多信息,您可以檢查以下答案: 檢測Android設備是否具有Internet連接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.