0

我想检查与 Android 中 LocationManager 的网络连接。我的代码适用于 Galaxy SII 和 4.0.3 版。但它不适用于 Galaxy S 和 2.3.6 版。

编码:

 isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
 isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

我肯定已连接到 Wi-Fi 网络。但是 if 语句

if (!isGPSEnabled && !isNetworkEnabled)

返回真。是否有已知的错误,是否有可靠的方法来检查它?

4

3 回答 3

1

试试这个:

    //If you only want to check your Internet connection available or not    
        public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
                        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                        return activeNetworkInfo != null && activeNetworkInfo.isConnected();}

注意wifiInfo为您提供有关您的Wifi的状态,但仍然会出现连接不佳的情况,我认为如果进行HTTP reuest会很好,有关更多详细信息,请查看提供的链接。

编辑:有关更多信息,您可以[参考此][1]。

于 2013-08-30T12:11:43.000 回答
0

I am using below code in my every android project. It will check for any active network that are used for outgoing connection.

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

        if (networkInfo != null && networkInfo.isConnectedOrConnecting())
            return true;

        return false;
    }
于 2013-08-30T12:14:46.990 回答
0

使用此代码:

 public static boolean checkConn(Context ctx)
{

    ConnectivityManager conMgr =  (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo wifiInfo = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo mobileInfo = conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);    
    NetworkInfo info = conMgr.getActiveNetworkInfo();

    if(info != null && info.isConnected()) {  
        Log.v("NetworkInfo","Connected State");  
        return true;
    }  
    return false;
}
于 2013-08-30T12:28:07.880 回答