簡體   English   中英

android互聯網連接可用性

[英]android internet connection avalability

在嘗試並在stackoverflow上搜索以解決android上的Internet連接錯誤的方法后,我什么都沒找到。 我嘗試了您可以在底部看到的代碼,但是它無法解決Internet連接錯誤。 mWebView.loadUrl("file:///android_asset/myerrorpage.html"); 每次在瀏覽器中的網址不是http://192./loc/index.php時都會執行。 當我在index.php上進行重定向時,將顯示錯誤文件。 我該如何更改它,或者知道任何人的代碼以檢查Internet連接可用性,然后執行某些操作?

    public boolean isOnline() {
            ConnectivityManager cm =
                (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            return netInfo != null && netInfo.isConnectedOrConnecting();
        }

    public boolean isInternetAvailable() {
            try {
                InetAddress ipAddr = InetAddress.getByName("google.com"); 
                if (ipAddr.equals("")) {
                    return false;
 mWebView.loadUrl("file:///android_asset/myerrorpage.html");
                } else {
                    return true;

@Override
        public void onCreate(Bundle savedInstanceState) {


            setContentView(R.layout.activity_localy);
            mWebView = (WebView) findViewById(R.id.webview);
            // Brower niceties -- pinch / zoom, follow links in place
            mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            mWebView.setWebViewClient(new GeoWebViewClient());
            // Below required for geolocation
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.getSettings().setGeolocationEnabled(true);
            mWebView.setWebChromeClient(new GeoWebChromeClient());     
            // Load google.com
            mWebView.loadUrl("http://192./loc/index.php");


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

        }

您的問題不是很清楚。 如果需要檢查連接,則需要:

public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();
}

然后,如果需要檢查真實的互聯網連接,則可以嘗試以下操作:

public boolean isInternetAvailable() {
        try {
            InetAddress ipAddr = InetAddress.getByName("google.com"); 
            if (ipAddr.equals("")) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            return false;
        }

    }

並將ACCESS_NETWORK_STATE權限添加到清單。

暫無
暫無

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

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