可检查包括蓝牙声像在内的Android

可检查包括蓝牙声像在内的Android

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

问题描述

我的应用程序在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;
}

我尝试运行其他一些应用程序.它们也没有显示网络连接,但是google应用程序可以完美运行,就像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.

这就是我解决的方法.

Created和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 Internet连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 12:13