Example usage for android.net NetworkInfo isAvailable

List of usage examples for android.net NetworkInfo isAvailable

Introduction

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

Prototype

@Deprecated
public boolean isAvailable() 

Source Link

Document

Indicates whether network connectivity is possible.

Usage

From source file:com.polyvi.xface.extension.inappbrowser.XInAppBrowserExt.java

/**
 * ?htpp???/*from   w  w  w.  j  a  v a2  s.c  om*/
 * @param url ??url
 * @return ?true?false
 */
private boolean isUrlAllowedLoad(String url) {
    //http??
    if (url.startsWith(XConstant.HTTP_SCHEME) || url.startsWith(XConstant.HTTPS_SCHEME)) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) getContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null) {
            return mNetworkInfo.isAvailable();
        } else {
            return false;
        }
    }
    return true;
}

From source file:org.thomasamsler.android.flashcards.activity.MainActivity.java

public boolean hasConnectivity() {

    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(
            Context.CONNECTIVITY_SERVICE);

    if (null == connectivityManager) {

        return false;
    }/*from w w w  .ja  v  a 2s . c  o m*/

    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    if (null != networkInfo && networkInfo.isAvailable() && networkInfo.isConnected()) {

        return true;
    } else {

        return false;
    }
}

From source file:com.alex.smartwomanmiddleeastfem.WebViewDemoActivity.java

/**
 * Checks networking status./*from   w ww  .java  2  s  .c  om*/
 */
private boolean checkConnectivity() {
    boolean enabled = true;

    ConnectivityManager connectivityManager = (ConnectivityManager) this
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectivityManager.getActiveNetworkInfo();

    if ((info == null || !info.isConnected() || !info.isAvailable())) {

    }
    return enabled;
}

From source file:org.pixmob.freemobile.netstat.SyncService.java

@Override
protected void onHandleIntent(Intent intent) {
    // Check if statistics upload is enabled.
    if (!prefs.getBoolean(Constants.SP_KEY_UPLOAD_STATS, false)) {
        Log.d(TAG, "Synchronization is disabled: skip sync");
        return;// www .  ja  va2s .  c  om
    }

    // Check if an Internet connection is available.
    final NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo == null || !netInfo.isAvailable() || !netInfo.isConnected()) {
        Log.d(TAG, "Network connectivity is not available: skip sync");
        return;
    }

    final PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    SQLiteDatabase db = null;
    try {
        wl.acquire();
        db = dbHelper.getWritableDatabase();
        run(intent, db);

        // Sync was successful: reset sync error count.
        internalPrefsEditor.remove(INTERNAL_SP_KEY_SYNC_ERRORS).commit();
    } catch (Exception e) {
        Log.e(TAG, "Failed to upload statistics", e);

        // Increment sync errors.
        final int syncErrors = internalPrefs.getInt(INTERNAL_SP_KEY_SYNC_ERRORS, 0);
        internalPrefsEditor.putInt(INTERNAL_SP_KEY_SYNC_ERRORS, syncErrors + 1).commit();
    } finally {
        if (db != null) {
            db.close();
        }
        wl.release();

        // Reschedule this service according to the sync error count.
        schedule(this, true);
    }

    Log.i(TAG, "Statistics upload done");
}

From source file:foam.littlej.android.app.net.MainHttpClient.java

/**
 * Is there internet connection/* w w w. j a  va 2s.  c om*/
 */
public boolean isConnected() {

    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    final NetworkInfo networkInfo;
    networkInfo = connectivity.getActiveNetworkInfo();
    // NetworkInfo info

    if (networkInfo != null && networkInfo.isConnected() && networkInfo.isAvailable()) {
        return true;
    }
    return false;

}

From source file:org.cocos2dx.plugin.ShareFacebook.java

private boolean networkReachable() {
    boolean bRet = false;
    try {/*from   w w  w .  j a  v a 2s.  c o  m*/
        ConnectivityManager conn = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = conn.getActiveNetworkInfo();
        bRet = (null == netInfo) ? false : netInfo.isAvailable();
    } catch (Exception e) {
        LogE("Fail to check network status", e);
    }
    LogD("NetWork reachable : " + bRet);
    return bRet;
}

From source file:com.danielme.muspyforandroid.activities.base.AbstractActivity.java

/**
 * Checks internet connection. Shows a dialog if it ain't available.
 *///from   w w  w  .  j  av  a2  s  . co m
public boolean checkConnection() {
    ConnectivityManager connectivityManager = (ConnectivityManager) this
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo info = connectivityManager.getActiveNetworkInfo();
    if ((noConnectionDialog == null || !noConnectionDialog.isShowing())
            && (info == null || !info.isConnected() || !info.isAvailable())) {
        Builder builder = new Builder(this);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setMessage(getString(R.string.noconnection));
        builder.setCancelable(false);
        builder.setNeutralButton(R.string.ok, null);
        builder.setTitle(getString(R.string.error));
        noConnectionDialog = builder.create();
        noConnectionDialog.show();
        dialogStyle(noConnectionDialog);
        return false;
    } else {
        return true;
    }

}

From source file:ti.modules.titanium.network.NetworkModule.java

@Kroll.getProperty
@Kroll.method/*from   www  .j  a va 2 s. c  o  m*/
public int getNetworkType() {
    int type = NETWORK_UNKNOWN;

    // start event needs network type. So get it if we don't have it.
    if (connectivityManager == null) {
        connectivityManager = getConnectivityManager();
    }

    try {
        NetworkInfo ni = connectivityManager.getActiveNetworkInfo();
        if (ni != null && ni.isAvailable() && ni.isConnected()) {
            type = networkTypeToTitanium(true, ni.getType());
        } else {
            type = NetworkModule.NETWORK_NONE;
        }
    } catch (SecurityException e) {
        Log.w(TAG, "Permission has been removed. Cannot determine network type: " + e.getMessage());
    }
    return type;
}

From source file:ti.modules.titanium.network.NetworkModule.java

@Kroll.getProperty
@Kroll.method//ww w .  j a  v  a2s .  c  om
public boolean getOnline() {
    boolean result = false;

    ConnectivityManager cm = getConnectivityManager();
    if (cm != null) {
        NetworkInfo ni = getConnectivityManager().getActiveNetworkInfo();

        if (ni != null && ni.isAvailable() && ni.isConnected()) {
            result = true;
        }
    } else {
        Log.w(TAG, "ConnectivityManager was null", Log.DEBUG_MODE);
    }
    return result;
}

From source file:com.fine47.http.ActivityHttpClient.java

/**
 * Checks whether the system is online (connected to a network) or not.
 *
 * @return TRUE if the system is online, FALSE otherwise
 *//*  w ww. j a va  2s. co m*/
public boolean isOnline() {
    try {
        ConnectivityManager cm = (ConnectivityManager) getContext().getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (null != netInfo) {
            // Check for availability and if it's really connected.
            isConnected = netInfo.isAvailable() && netInfo.isConnectedOrConnecting();

            // Get available networks' info.
            NetworkInfo[] netsInfo = cm.getAllNetworkInfo();

            // What kind of networks are available.
            for (NetworkInfo ni : netsInfo) {
                if (ni.isConnected()) {
                    String niType = ni.getTypeName();
                    if ("WIFI".equalsIgnoreCase(niType)) {
                        isWifiConnected = true;
                    } else if ("MOBILE".equalsIgnoreCase(niType)) {
                        isMobileConnected = true;
                    }
                }
            }
        } else {
            isConnected = false;
        }

        return isConnected;
    } catch (Throwable error) {
        if (isDebugging()) {
            Log.e(LOG_TAG, "Error while detecting network status.", error);
        }
    }

    return isConnected;
}