即使没有网络,也总是在手机上检测到网络连接

时间:2011-04-29 05:30:46

标签: android networking

我正在检查Android手机上的网络连接。即使我在“设置 - >中关闭网络连接,它也始终显示”Internet Connection Present“。位置 - >使用无线网络 - >关闭“和”启用GPS状态 - >关闭“并禁用wifi。

我使用以下代码:

  private boolean checkInternetConnection() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        // test for connection
        if (cm.getActiveNetworkInfo() != null
                && cm.getActiveNetworkInfo().isAvailable()
                && cm.getActiveNetworkInfo().isConnected()) {
            Log.e("TAG", "Internet Connection Present");
            return true;
        } else {
            Log.e("TAG", "Internet Connection Not Present");
            return false;
        }

    }

我已设置权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

我做错了什么?

3 个答案:

答案 0 :(得分:1)

public static boolean isNetworkAvailable(Context context)
{
    ConnectivityManager connectivity = (ConnectivityManager)context.getSystemServic(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) 
    {
        Log.w("tag", "couldn't get connectivity manager");
    }
    else {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
        return false;
}

答案 1 :(得分:1)

'设置 - &gt;位置 - &gt;使用无线网络(关闭)和使用GPS卫星(关闭)'如上所述是用于确定位置的设置,而不是禁用对互联网的访问。

要完全切断对互联网的访问,请尝试飞行模式(设置 - &gt;无线和网络设置 - &gt;飞行模式)

答案 2 :(得分:0)

/**
     * THIS IS FUNCTION FOR CHECKING INTERNET CONNECTION
     * @return TRUE IF INTERNET IS PRESENT ELSE RETURN FALSE
     */
    public boolean checkInternetConnection() {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected()) {
            return true;

        } else {
            return false;
        }
    }

这适用于我的项目并在设备上进行了测试

如果您想查看GPS提供商的可用性,请尝试使用此代码

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        String provider = null;

        if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            provider = LocationManager.NETWORK_PROVIDER;

        } else if ( lm.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            provider = LocationManager.GPS_PROVIDER ;

        } else {
            Toast.makeText(this, "Provider Not available", 3000).show();
        }

        if(provider != null) {


            lm.requestLocationUpdates(provider, 30000, 100, this);
            Location location = lm.getLastKnownLocation(provider);

            if(location != null) {
                currentGeopoint = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6));

                if(currentGeopoint != null) {
                    currentaddress = getAddressFromLatitudeAndLongitude(location.getLatitude(), location.getLongitude());
                }
            }

        }
相关问题