Android Network State Check isInternetReachable(Context context)

Here you can find the source of isInternetReachable(Context context)

Description

is Internet Reachable

License

Open Source License

Declaration

public static Boolean isInternetReachable(Context context) 

Method Source Code

//package com.java2s;
/**/*from  w ww . j  a va  2s  . c  o m*/
 * This file is part of LoLin1.
 * <p/>
 * LoLin1 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * <p/>
 * LoLin1 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * <p/>
 * You should have received a copy of the GNU General Public License
 * along with LoLin1. If not, see <http://www.gnu.org/licenses/>.
 * <p/>
 * Created by Jorge Antonio Diaz-Benito Soriano on 03/01/14.
 * <p/>
 * Accessing resources through reflection is said to be ten times faster than through getResources(), and thus it's done when possible.
 */

import android.content.Context;

import android.content.SharedPreferences;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import android.preference.PreferenceManager;

public class Main {
    public static Boolean isInternetReachable(Context context) {
        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        Boolean ret;

        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiNetworkInfo = connectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI), dataNetworkInfo = connectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        Boolean isWifiConnected = (wifiNetworkInfo == null) ? Boolean.FALSE
                : wifiNetworkInfo.isConnected(), isDataConnected = (dataNetworkInfo == null) ? Boolean.FALSE
                : dataNetworkInfo.isConnected();
        ret = isWifiConnected
                || (preferences
                        .getBoolean("pref_title_data", Boolean.FALSE) && isDataConnected);

        return ret;
    }
}

Related

  1. isConnectNetWork(final Context context)
  2. isOnline(Context context)
  3. isNetworkAvailable(Context context)
  4. isNetworkAvailable(Context context)
  5. isMobileOnline(Context context)
  6. isHightBandwidthConnection(Context context)
  7. isConnectionFast(int type, int subType)
  8. isNetAvailable(Context context)
  9. isNetworkAvailable(Context context)