【问题标题】:Android Internet Connection timeoutAndroid Internet 连接超时
【发布时间】:2017-09-05 00:55:12
【问题描述】:

我可以使用

检测互联网连接
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]

我怎样才能实现它。

【问题讨论】:

    标签: java android android-internet android-connectivitymanager


    【解决方案1】:

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

    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】:

      你是对的,你可以检查你是否连接到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;
      }
      

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

      有关更多信息,您可以查看此答案: Detect if Android device has Internet connection

      【讨论】:

        【解决方案3】:

        您可以通过使用处理程序来实现此目的

        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

        【讨论】:

        • 感谢您的 cmets,让我试试这个。
        • @ChandraMouliPoreddy 当然,伙计,如果它有效,那么不要忘记接受并评价答案。 :)
        猜你喜欢
        • 1970-01-01
        • 2012-10-26
        • 2013-12-01
        • 2018-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-17
        • 2016-10-02
        相关资源
        最近更新 更多