2

我能够使用检测互联网连接

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]

我怎样才能实现它。

4

3 回答 3

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;
        }
    }
于 2017-04-10T05:28:06.563 回答
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

于 2017-04-10T05:19:52.373 回答
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;
}

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

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

于 2017-04-10T05:51:28.943 回答