Android Internet连接超时

时间:2017-04-10 05:13:29

标签: java android android-internet android-connectivitymanager

我能够使用

检测互联网连接
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秒后向用户显示一条消息[无互联网检查您的连接]

我怎样才能实现它。

3 个答案:

答案 0 :(得分:0)

您可以使用处理程序

来实现此目的
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

答案 1 :(得分:0)

您可以尝试以下方法检查互联网连接,然后在没有互联网的情况下显示祝酒词:

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;
        }
    }

答案 2 :(得分:0)

你是对的,你可以检查一下你是否与wifiCellular 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;
}

您可以将网址更改为任何所需的地址,但此网址会生成确定的响应,因此您不必担心服务器问题。当然您可以将其更改为您的服务器以检查服务器的可用性^^

有关详细信息,请查看以下答案: Detect if Android device has Internet connection