Example usage for android.net NetworkInfo isConnected

List of usage examples for android.net NetworkInfo isConnected

Introduction

In this page you can find the example usage for android.net NetworkInfo isConnected.

Prototype

@Deprecated
public boolean isConnected() 

Source Link

Document

Indicates whether network connectivity exists and it is possible to establish connections and pass data.

Usage

From source file:at.metalab.donarsson.screeninvader.InvadeScreen.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(
            Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (networkInfo.isConnected()) {
        //TODO: Check if we know a ScreenInvader on this network
        Intent intent = getIntent();/*www  . j  av a 2  s . c om*/
        String type = intent.getType();
        if (type.startsWith("text/")) {
            String text = intent.getStringExtra(Intent.EXTRA_TEXT);
            Pattern pattern = Patterns.WEB_URL;
            Matcher matcher = pattern.matcher(text);
            while (matcher.find()) {
                String url = matcher.group();
                new PostUrlTask().execute(url);
            }
        } //TODO: Add support for other types (file upload)
    } else {
        //TODO: Display a prompt to connect to a WiFi
        Toast.makeText(getApplicationContext(), getString(R.string.no_wifi_toast), Toast.LENGTH_LONG).show();
    }
    finish();
}

From source file:de.micmun.android.workdaystarget.DaysLeftService.java

/**
 * Returns <code>true</code>, if you are connected to the internet.
 *
 * @return <code>true</code>, if connected to the internet.
 *///from   w ww . j a  v  a 2 s .  c om
private boolean isOnline() {
    boolean ret = false;
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();

    if (ni != null && ni.isConnected() && !ni.isRoaming())
        ret = true;

    return ret;
}

From source file:ru.neverdark.phototools.azimuth.model.Geocoder.java

/**
 * Checks connection status/* w  ww.  ja va 2 s  .c  o  m*/
 *
 * @return true if device online, false in other case
 */
public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()) {
        return true;
    }
    return false;
}

From source file:weavebytes.com.futureerp.activities.DashBoardActivity.java

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()) {
        try {//from w ww . j  a va  2  s .c o  m
            URL url = new URL("http://www.google.com");
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setConnectTimeout(2000);
            urlc.connect();
            if (urlc.getResponseCode() == 200) {
                return new Boolean(true);
            }
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return false;
}

From source file:com.teinproductions.tein.papyrosprogress.MilestoneLoader.java

@Override
public Result loadInBackground() {
    ConnectivityManager connManager = (ConnectivityManager) getContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connManager.getActiveNetworkInfo();

    if (networkInfo == null || !networkInfo.isConnected()) {
        Result result = new Result();
        result.setError(Result.NO_INTERNET_CONNECTION);
        return result;
    }//from w  w  w  .jav  a  2  s . c o m

    Result result = IOUtils.loadPage(Constants.MILESTONES_URL);
    if (result.getError() == Result.NO_ERROR) {
        try {
            result.setData(JSONUtils.getMilestones(result.getStrData()));
        } catch (ParseException | JSONException | NullPointerException e) {
            e.printStackTrace();
            result.setError(Result.JSON_PARSE_ERROR);
        }
    }
    return result;
}

From source file:androidx.work.impl.constraints.trackers.NetworkStateTracker.java

private NetworkState getActiveNetworkState() {
    // Use getActiveNetworkInfo() instead of getNetworkInfo(network) because it can detect VPNs.
    NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
    boolean isConnected = info != null && info.isConnected();
    boolean isValidated = isActiveNetworkValidated();
    boolean isMetered = ConnectivityManagerCompat.isActiveNetworkMetered(mConnectivityManager);
    boolean isNotRoaming = info != null && !info.isRoaming();
    return new NetworkState(isConnected, isValidated, isMetered, isNotRoaming);
}

From source file:com.chainsaw.clearweather.BackgroundFetch.java

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) appContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

From source file:ru.neverdark.phototools.azimuth.model.GoogleTimeZone.java

/**
 * Checks connection status/*  w  ww.  j  a v a2  s . c  om*/
 *
 * @return true if device online, false in other case
 */
private boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()) {
        return true;
    }
    return false;
}

From source file:com.afwsamples.testdpc.safetynet.SafetyNetFragment.java

private boolean hasInternetConnection() {
    ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return activeNetwork != null && activeNetwork.isConnected();
}