简体   繁体   English

android互联网连接可用性

[英]android internet connection avalability

After trying and searching on stackoverflow for ways to solve internet connection error on android, I found nothing what works for me. 在尝试并在stackoverflow上搜索以解决android上的Internet连接错误的方法后,我什么都没找到。 I tryed the code you can see at the bottom but it wont works for internet connection error. 我尝试了您可以在底部看到的代码,但是它无法解决Internet连接错误。 mWebView.loadUrl("file:///android_asset/myerrorpage.html"); become executed every time when the url in the browser isnt http://192./loc/index.php . 每次在浏览器中的网址不是http://192./loc/index.php时都会执行。 When I have a redirecting at index.php the error file become showed. 当我在index.php上进行重定向时,将显示错误文件。 How can I change that, or know anyone a code that check the internet connection availability and then do something? 我该如何更改它,或者知道任何人的代码以检查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;
            }

        }

Your question isn't very clear. 您的问题不是很清楚。 If you need check connection you need: 如果需要检查连接,则需要:

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

Then if need check for real internet connection you can try something like this: 然后,如果需要检查真实的互联网连接,则可以尝试以下操作:

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

    }

And add the ACCESS_NETWORK_STATE permission to the manifest. 并将ACCESS_NETWORK_STATE权限添加到清单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM