Problem in detecting Internet Connection in Android

前端 未结 2 1944
终归单人心
终归单人心 2020-12-15 02:07

I have created a Android application using Google Maps. Now I want to check whether an internet connection is available or not. I search in Google and finally I got solution

相关标签:
2条回答
  • 2020-12-15 02:20
    /**
     * Checking whether net connection is available or not.
     * 
     * @param nContext
     * @return true if net connection is avaible otherwise false
     */
    public static boolean isNetworkAvailable(Context nContext) {
        boolean isNetAvailable = false;
        if (nContext != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) nContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            if (mConnectivityManager != null) {
                boolean mobileNetwork = false;
                boolean wifiNetwork = false;
                boolean mobileNetworkConnecetd = false;
                boolean wifiNetworkConnecetd = false;
                NetworkInfo mobileInfo = mConnectivityManager
                        .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                NetworkInfo wifiInfo = mConnectivityManager
                        .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                if (mobileInfo != null)
                    mobileNetwork = mobileInfo.isAvailable();
                if (wifiInfo != null)
                    wifiNetwork = wifiInfo.isAvailable();
                if (wifiNetwork == true || mobileNetwork == true) {
                    if (mobileInfo != null)
                        mobileNetworkConnecetd = mobileInfo
                                .isConnectedOrConnecting();
                    wifiNetworkConnecetd = wifiInfo.isConnectedOrConnecting();
                }
                isNetAvailable = (mobileNetworkConnecetd || wifiNetworkConnecetd);
            }
        }
        return isNetAvailable;
    
    }
    

    Also add below tag in manifest permission:

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
    0 讨论(0)
  • 2020-12-15 02:39

    Try using NetworkInfo ni = cm.getActiveNetworkInfo(); instead of NetworkInfo ni = cm.getAllNetworkInfo();. There should only be one active data network at any one time because Android will use the best available and shut down the others to conserve battery.

    Also, I tend to use ni.isConnectedOrConnecting(); instead of ni.isConnected(); because it can catch transition states better.

    I also use ni.getType() == ConnectivityManager.TYPE_WIFI instead of ni.getTypeName().equalsIgnoreCase("WIFI") because it is much more efficient to compare two int values than it is to compare two strings.

    The following code works for me:

    boolean HaveConnectedWifi = false;
    boolean HaveConnectedMobile = false;
    ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if ( ni != null )
    {
        if (ni.getType() == ConnectivityManager.TYPE_WIFI)
            if (ni.isConnectedOrConnecting())
                HaveConnectedWifi = true;
        if (ni.getType() == ConnectivityManager.TYPE_MOBILE)
            if (ni.isConnectedOrConnecting())
                HaveConnectedMobile = true;
    }
    
    0 讨论(0)
提交回复
热议问题