簡體   English   中英

Android網絡檢查在某些設備中不起作用

[英]Android network checking is not working in some devices

您好,我是android的新手,我實現了一種檢查Internet連接的方法。它在某些設備上可以正常工作,但在三星銀河標簽4中無法正常工作。應用程序。它在所有其他設備上都能正常運行。 請事先幫助我

public boolean isConnected() {
        try {

            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            if (cm.getActiveNetworkInfo() != null
                    && cm.getActiveNetworkInfo().isConnectedOrConnecting()) {
                // Network is available but check if we can get access from the
                // network.
                URL url = new URL("http://www.google.co.in");
                HttpURLConnection urlc = (HttpURLConnection) url
                        .openConnection();
                urlc.setRequestProperty("Connection", "close");
                urlc.setConnectTimeout(2000); // Timeout 2 seconds.
                urlc.connect();

                if (urlc.getResponseCode() == 200) // Successful response.
                {
                    Log.d("response", "" + urlc.getResponseCode());
                    return true;
                } else {

                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }  

嘗試:

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnected();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM