Example usage for android.net ConnectivityManager TYPE_WIFI

List of usage examples for android.net ConnectivityManager TYPE_WIFI

Introduction

In this page you can find the example usage for android.net ConnectivityManager TYPE_WIFI.

Prototype

int TYPE_WIFI

To view the source code for android.net ConnectivityManager TYPE_WIFI.

Click Source Link

Document

A WIFI data connection.

Usage

From source file:cw.kop.autobackground.files.FileHandler.java

public static boolean download(Context appContext) {

    if (!isDownloading) {
        ConnectivityManager connect = (ConnectivityManager) appContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo wifi = connect.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = connect.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi != null && wifi.isConnected() && AppSettings.useWifi()) {
        } else if (mobile != null && mobile.isConnected() && AppSettings.useMobile()) {
        } else {/*  w w w. j  a v a2s  . c o  m*/
            if (AppSettings.useToast()) {
                Toast.makeText(appContext, "No connection available,\ncheck Download Settings",
                        Toast.LENGTH_SHORT).show();
            }

            Intent resetDownloadIntent = new Intent(FileHandler.DOWNLOAD_TERMINATED);
            LocalBroadcastManager.getInstance(appContext).sendBroadcast(resetDownloadIntent);
            return false;
        }
        isDownloading = true;
        downloadThread = new DownloadThread(appContext);
        downloadThread.start();
        if (AppSettings.useToast()) {
            Toast.makeText(appContext, "Downloading images", Toast.LENGTH_SHORT).show();
        }
        return true;
    } else {
        return false;
    }
}

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;/*  ww w . j a  v  a2s.c o 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:org.basdroid.common.NetworkUtils.java

/**
 * Check if there is any connectivity to a Wifi network
 * @param context/*from w  w  w .j  av  a  2 s . co  m*/
 * @return
 */
public static boolean isConnectedWifi(Context context) {
    NetworkInfo info = getNetworkInfo(context);
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}

From source file:com.air.mobilebrowser.NetworkUtil.java

/**
 * Check the current status of internet connectivity.
 * This method iterates over the available network interfaces and
 * checks for an active connection.//  w  ww .  j  a v a  2s  . c om
 * @return true if a connection was detected, false otherwise.
 */
public static boolean haveInternetConnection(Context context) {
    if (context != null) {
        ConnectivityManager connectivityMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo wifiInfo = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wimax = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIMAX);
        NetworkInfo blue = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_BLUETOOTH);
        NetworkInfo ether = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);

        boolean hasInternet = false;

        if (wifiInfo != null && wifiInfo.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        } else if (mobile != null && mobile.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        } else if (wimax != null && wimax.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        } else if (blue != null && blue.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        } else if (ether != null && ether.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        }

        return hasInternet;
    }

    return false;
}

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

public static Boolean isNetAvailable(Context con) {
    try {//w ww.  j  a v  a2s.c om
        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:com.frame.network.utils.NetworkUtil.java

/**
 * WIFI??/*from   w  w  w  . j  a  va2 s  . c  o  m*/
 * @param context
 * @return
 */
public static boolean isWifiAwailable(Context context) {
    if (context == null) {
        return false;
    }
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (info == null) {
        return false;
    }
    return info.isAvailable();
}

From source file:com.lee.sdk.utils.NetUtils.java

/**
 * wifi??//from w ww.j a  v a2 s  .c o m
 * 
 * @param context context
 * @return wifi? true
 */
public static boolean isWifiNetworkConnected(Context context) {
    NetworkInfo networkInfo = getActiveNetworkInfo(context);
    // return networkInfo != null && networkInfo.isConnected();
    boolean flag = networkInfo != null && networkInfo.isAvailable()
            && networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
    if (DEBUG) {
        Log.d(TAG, "isWifiNetworkConnected, rtn: " + flag);
    }
    return flag;

}

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();/*from w w  w. jav a2  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:com.example.admin.processingboilerplate.MainActivity.java

public static boolean isConnectedWifi() {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm == null ? null : cm.getActiveNetworkInfo();
    return (networkInfo != null && networkInfo.isConnected()
            && networkInfo.getType() == ConnectivityManager.TYPE_WIFI);
}

From source file:com.frame.network.utils.NetworkUtil.java

/**
 * WIFI?/*  w w  w .j  a v a 2s .co m*/
 * @param context
 * @return
 */
public static boolean isWifiConnected(Context context) {
    if (context == null) {
        return false;
    }
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (info == null) {
        return false;
    }
    return info.isConnected();
}