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:com.harshad.linconnectclient.NotificationUtilities.java

public static boolean sendData(Context c, Notification n, String packageName) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);

    ConnectivityManager connManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    // Check Wifi state, whether notifications are enabled globally, and
    // whether notifications are enabled for specific application
    if (prefs.getBoolean("pref_toggle", true) && prefs.getBoolean(packageName, true) && mWifi.isConnected()) {
        String ip = prefs.getString("pref_ip", "0.0.0.0:9090");

        // Magically extract text from notification
        ArrayList<String> notificationData = NotificationUtilities.getNotificationText(n);

        // Use PackageManager to get application name and icon
        final PackageManager pm = c.getPackageManager();
        ApplicationInfo ai;/*  w  w w .j  a  v  a 2  s. co m*/
        try {
            ai = pm.getApplicationInfo(packageName, 0);
        } catch (final NameNotFoundException e) {
            ai = null;
        }

        String notificationBody = "";
        String notificationHeader = "";
        // Create header and body of notification
        if (notificationData.size() > 0) {
            notificationHeader = notificationData.get(0);
            if (notificationData.size() > 1) {
                notificationBody = notificationData.get(1);
            }
        } else {
            return false;
        }

        for (int i = 2; i < notificationData.size(); i++) {
            notificationBody += "\n" + notificationData.get(i);
        }

        // Append application name to body
        if (pm.getApplicationLabel(ai) != null) {
            if (notificationBody.isEmpty()) {
                notificationBody = "via " + pm.getApplicationLabel(ai);
            } else {
                notificationBody += " (via " + pm.getApplicationLabel(ai) + ")";
            }
        }

        // Setup HTTP request
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        // If the notification contains an icon, use it
        if (n.largeIcon != null) {
            entity.addPart("notificon",
                    new InputStreamBody(ImageUtilities.bitmapToInputStream(n.largeIcon), "drawable.png"));
        }
        // Otherwise, use the application's icon
        else {
            entity.addPart("notificon",
                    new InputStreamBody(
                            ImageUtilities.bitmapToInputStream(
                                    ImageUtilities.drawableToBitmap(pm.getApplicationIcon(ai))),
                            "drawable.png"));
        }

        HttpPost post = new HttpPost("http://" + ip + "/notif");
        post.setEntity(entity);

        try {
            post.addHeader("notifheader", Base64.encodeToString(notificationHeader.getBytes("UTF-8"),
                    Base64.URL_SAFE | Base64.NO_WRAP));
            post.addHeader("notifdescription", Base64.encodeToString(notificationBody.getBytes("UTF-8"),
                    Base64.URL_SAFE | Base64.NO_WRAP));
        } catch (UnsupportedEncodingException e) {
            post.addHeader("notifheader",
                    Base64.encodeToString(notificationHeader.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP));
            post.addHeader("notifdescription",
                    Base64.encodeToString(notificationBody.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP));
        }

        // Send HTTP request
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        try {
            response = client.execute(post);
            String html = EntityUtils.toString(response.getEntity());
            if (html.contains("true")) {
                return true;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    return false;
}

From source file:Main.java

public static boolean ensureWifi(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo == null || !netInfo.isConnectedOrConnecting())
        return false;
    // always OK if we're on wifi
    if (netInfo.getType() == ConnectivityManager.TYPE_WIFI)
        return true;
    // check for wifi only pref
    if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean("wifiPref", true)) {
        Log.d("Podax", "Not downloading because Wifi is required and not connected");
        return false;
    }/*from   www .j a v  a 2  s  . c  o  m*/
    // check for 3g data turned off
    if (!netInfo.isConnected()) {
        Log.d("Podax", "Not downloading because background data is turned off");
        return false;
    }

    return true;
}

From source file:com.ichi2.async.Connection.java

public static boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) AnkiDroidApp.getInstance().getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
        return false;
    }//from  ww w  . j  av a2s .  com
    return true;
}

From source file:de.da_sense.moses.client.service.MosesService.java

/**
 * Checks if the device is connected to the Internet.
 * //from   w w w .  j  a v  a 2 s.  c o  m
 * @return true if the device is connected, false otherwise
 */
public static boolean isOnline(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()) {
        return true;
    }
    return false;
}

From source file:com.adam.aslfms.util.Util.java

public static NetworkStatus checkForOkNetwork(Context ctx) {

    AppSettings settings = new AppSettings(ctx);
    PowerOptions powerOptions = checkPower(ctx);

    NetworkOptions networkOptions = settings.getNetworkOptions(powerOptions);
    boolean roaming = settings.getSubmitOnRoaming(powerOptions);

    ConnectivityManager connectivityManager = (ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
    if (netInfo == null) {
        Log.d(TAG, "netInfo == null");
    }/*from  www  . j a v  a 2 s  .  co m*/
    if (netInfo != null) {
        Log.d(TAG, "conn: " + netInfo.isConnected() + " : " + netInfo.toString());
    }

    if (netInfo == null || !netInfo.isConnected()) {
        return NetworkStatus.DISCONNECTED;
    }

    if (netInfo.isRoaming() && !roaming) {
        return NetworkStatus.UNFIT;
    }

    int netType = netInfo.getType();
    int netSubType = netInfo.getSubtype();

    Log.d(TAG, "netType: " + netType);
    Log.d(TAG, "netSubType: " + netSubType);

    if (networkOptions.isNetworkTypeForbidden(netType)) {
        Log.d(TAG, "Network type forbidden");
        return NetworkStatus.UNFIT;
    }
    if (networkOptions.isNetworkSubTypeForbidden(netType, netSubType)) {
        Log.d(TAG, "Network sub type forbidden");
        return NetworkStatus.UNFIT;
    }

    return NetworkStatus.OK;
}

From source file:com.ppshein.sevendaynews.common.java

public static Boolean isNetAvailable(Context con) {
    try {//from   w ww .  j  a  v  a 2s. c  o m
        ConnectivityManager connectivityManager = (ConnectivityManager) con
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (wifiInfo.isConnected() || mobileInfo.isConnected()) {
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:dk.dr.radio.diverse.App.java

public static boolean erOnline() {
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        return (networkInfo != null && networkInfo.isConnected());
    }//  ww w  .  j a  va 2s . c  om

From source file:com.amazonaws.mobileconnectors.cognito.DefaultDataset.java

static boolean isNetworkAvailable(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null) {
        return false;
    }//from   w w  w . ja v a2 s.  c  om
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return activeNetwork != null && activeNetwork.isConnected();
}

From source file:com.mobile.natal.natalchart.NetworkUtilities.java

/**
 * Checks is the network is available./*from w ww .ja v  a 2  s. c  o m*/
 *
 * @param context the {@link Context}.
 * @return true if the network is available.
 */
public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        return false;
    } else {
        NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
        return (networkInfo != null && networkInfo.isConnected());
    }
}

From source file:com.andrewshu.android.reddit.common.Common.java

public static boolean shouldLoadThumbnails(Activity activity, RedditSettings settings) {
    //check for wifi connection and wifi thumbnail setting
    boolean thumbOkay = true;
    if (settings.isLoadThumbnailsOnlyWifi()) {
        thumbOkay = false;/*w ww.  j  ava 2s  . com*/
        ConnectivityManager connMan = (ConnectivityManager) activity
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = connMan.getActiveNetworkInfo();
        if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI && netInfo.isConnected()) {
            thumbOkay = true;
        }
    }
    return settings.isLoadThumbnails() && thumbOkay;
}