检查Android互联网连接的完美功能,包括蓝牙平台

JIt*_*hin 14 android android-bluetooth

我的应用程序在wifi和移动网络中工作得很完美,但无法检测何时通过蓝牙网络共享连接.

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

    if (networkInfo != null && networkInfo.isConnected()) {
        return true;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

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

任何人都可以告诉我一种方法来检查Android中的互联网连接,通过所有可用的方式,包括蓝牙泛和代理等.

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

Muz*_*ant 7

尝试连接到"始终可用"的网站.如果存在任何连接,则应该返回true:

protected static 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
        return false;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

  • @FernandoCarvalhosa这取决于你的情况.我看到它的方式,在大多数情况下等待响应返回不是问题(通常,它在几毫秒内返回,甚至在没有连接时返回).检查连接状态不能解答"我是否有Internet连接?"的问题.当您连接到无法访问Internet或通过蓝牙连接的WiFi连接时 (2认同)

JIt*_*hin 1

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

我就是这样解决的。

创建 AsyncTask 以避免主线程上的网络异常。

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;
}
Run Code Online (Sandbox Code Playgroud)

}

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

    // 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;
    }
Run Code Online (Sandbox Code Playgroud)