本文介绍了检查Android互联网连接的完美功能,包括蓝牙盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在 wifi 和移动网络中运行良好,但在通过蓝牙网络共享连接时无法检测到.

My application is working perfect in wifi and mobile networks, but fails to detect when connected through bluetooth tethering.

public boolean isNetworkAvailable() {
    ConnectivityManager cm = (ConnectivityManager)
      getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected()) {
        return true;
    }
    return false;
}

我尝试运行其他一些应用程序.他们也显示没有网络连接,但谷歌应用程序运行完美,其他一些应用程序如 whatsap 也是如此.想知道他们是如何做到的,以及为什么大多数应用程序都忽略了这一点..

I tried running a few other applications . they also shows no network connectivity, But google applications works perfect and so as some other apps like whatsap. Wondering how they are doing it, and why most of the applications missing this point..

谁能告诉我一种在android中检查互联网连接的方法,通过所有可用的方式,包括蓝牙盘和代理等.

Can anyone tell me a way to check the internet connectivity in android, through all means available,including bluetooth pan and proxy,etc.

任何帮助将不胜感激.提前致谢..

Any help will be appreciated. Thanks in advance..

推荐答案

我同意 Muzikant 的观点并感谢您的想法.我认为发布实施的解决方案会更好,因为它需要一些补充.

I agree with Muzikant and thanks for the idea. I thought it will be better to post the implemented solution as it needs some additions.

我就是这样解决的.

创建和 AsyncTask 来避免主线程异常的网络.

Created and AsyncTask to avoid network on main thread exception.

public class GetInternetStatus extends AsyncTask<Void,Void,Boolean> {

@Override
protected Boolean doInBackground(Void... params) {

    return hasInternetAccess();
}

protected  boolean hasInternetAccess()
{

    try
    {
        URL url = new URL("http://www.google.com");

        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
        urlc.setRequestProperty("User-Agent", "Android Application:1");
        urlc.setRequestProperty("Connection", "close");
        urlc.setConnectTimeout(1000 * 30);
        urlc.connect();

        // http://www.w3.org/Protocols/HTTP/HTRESP.html
        if (urlc.getResponseCode() == 200 || urlc.getResponseCode() > 400)
        {
            // Requested site is available
            return true;
        }
    }
    catch (Exception ex)
    {
        // Error while trying to connect
        ex.printStackTrace();
        return false;
    }
    return false;
}

}

现在将以下函数添加到活动并调用它来检查连接.

Now add the following function to activity and call it to check the connectivity.

    // Checking for all possible internet connections
    public static boolean isConnectingToInternet() {
        Boolean result = false;
        try {
            //get the result after executing AsyncTask
            result = new GetInternetStatus().execute().get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return result;
    }

这篇关于检查Android互联网连接的完美功能,包括蓝牙盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 08:39