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:com.tbay.android.tcpclient.MainActivity.java

/**
 * Check whether the device is connected, and if so, whether the connection
 * is wifi or mobile (it could be something else).
 *//*from  w  w  w.j av a 2s. c  o m*/
private void checkNetworkConnection() {
    // BEGIN_INCLUDE(connect)
    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
    if (activeInfo != null && activeInfo.isConnected()) {
        wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
        mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
        if (wifiConnected) {
            Log.i(TAG, getString(R.string.wifi_connection));
        } else if (mobileConnected) {
            Log.i(TAG, getString(R.string.mobile_connection));
        }
    } else {
        Log.i(TAG, getString(R.string.no_wifi_or_mobile));
    }
    // END_INCLUDE(connect)
}

From source file:com.heneryh.aquanotes.io.ApexExecutor.java

/**
 * Execute a {@link HttpGet} request, passing a valid response through
 * to the specified XML parser.  This common method can then be used to parse
 * various kinds of XML feeds.//from  w  ww .  j  ava2  s  .c  o  m
 */
public void executeGet(Uri ctrlUri, DefaultHandler xmlParser) throws HandlerException {

    controllerUri = ctrlUri;
    Cursor cursor = null;

    String username = null;
    String password = null;
    String apexBaseURL = null;
    String apexWANURL = null;
    String apexWiFiURL = null;
    String apexWiFiSID = null;
    String controllerType = null;

    /**
     * Poll the database for facts about this controller
     */
    try {
        cursor = mDbResolver.query(controllerUri, ControllersQuery.PROJECTION, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            username = cursor.getString(ControllersQuery.USER);
            password = cursor.getString(ControllersQuery.PW);
            apexWANURL = cursor.getString(ControllersQuery.WAN_URL);
            apexWiFiURL = cursor.getString(ControllersQuery.LAN_URL);
            apexWiFiSID = cursor.getString(ControllersQuery.WIFI_SSID);
            controllerType = cursor.getString(ControllersQuery.MODEL);
        }
    } catch (SQLException e) {
        throw new HandlerException("Database error getting controller data.");
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    /**
     * Depending on whether or not we are on the 'Home' wifi network we want to use either the
     * WAN or LAN URL.
     * 
     * Uhg, WifiManager stuff below crashes if wifi not enabled so first we have to check if
     * on wifi.
     */
    ConnectivityManager cm = (ConnectivityManager) mActContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    if (netInfo.getType() == ConnectivityManager.TYPE_WIFI) {
        /**
         * Get the currently connected SSID, if it matches the 'Home' one then use the local WiFi URL rather than the public one
         */
        WifiManager wm = (WifiManager) mActContext.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wInfo = wm.getConnectionInfo();

        // somewhere read this was a quoted string but appears not to be
        if (wInfo.getSSID().equalsIgnoreCase(apexWiFiSID)) {
            apexBaseURL = apexWiFiURL;
        } else {
            apexBaseURL = apexWANURL;
        }
    } else {
        apexBaseURL = apexWANURL;
    }

    /**
     * for this function we need to append to the URL.  To be safe we try to catch various
     * forms of URL that might be entered by the user:
     * 
     * check if the "/" was put on the end
     */
    if (!apexBaseURL.endsWith("/")) {
        String tmp = apexBaseURL + "/";
        apexBaseURL = tmp;
    }

    /**
     * check if it starts with an "http://"
     */
    if (!apexBaseURL.toLowerCase().startsWith("http://")) {
        String tmp = "http://" + apexBaseURL;
        apexBaseURL = tmp;
    }

    // oh, we should also check if it ends with an "status.sht" on the end and remove it.

    /**
     * When all cleaned up, add the xml portion of the url to grab the status.
     * 
     * TODO: we tried to make this call handle various xml feeds but this call is hardcoded
     * for the status feed.
     */
    String apexURL = apexBaseURL + "cgi-bin/status.xml";

    final HttpUriRequest request = new HttpGet(apexURL);
    executeWhySeparate(request, xmlParser, username, password);
}

From source file:org.gc.networktester.util.Util.java

public static int getTimingResource(MainActivity mainAct, long timing) {
    if (mainAct.getNetworkType() == null) {
        return 0;
    } else if (mainAct.getNetworkType() == ConnectivityManager.TYPE_MOBILE) {
        if (timing < mobileThresholds[0]) {
            return R.drawable.timing_good;
        } else if (timing < mobileThresholds[1]) {
            return R.drawable.timing_medium;
        } else {//  w w  w .ja  va2s .c o m
            return R.drawable.timing_bad;
        }
    } else if (mainAct.getNetworkType() == ConnectivityManager.TYPE_WIFI || mainAct.getNetworkType() == 6) { // ConnectivityManager.TYPE_WIMAX since API level 8
        if (timing < wifiThresholds[0]) {
            return R.drawable.timing_good;
        } else if (timing < wifiThresholds[1]) {
            return R.drawable.timing_medium;
        } else {
            return R.drawable.timing_bad;
        }
    } else {
        Toast.makeText(mainAct, R.string.error_unknown_network_type, Toast.LENGTH_LONG).show();
        return 0;
    }
}

From source file:dev.ukanth.ufirewall.RulesActivity.java

protected void appendSystemInfo(final Context ctx) {
    // Fourth section: "System info"
    writeHeading(result, true, "System info");

    InterfaceDetails cfg = InterfaceTracker.getCurrentCfg(ctx);

    result.append("Android version: " + android.os.Build.VERSION.RELEASE + "\n");
    result.append("Manufacturer: " + android.os.Build.MANUFACTURER + "\n");
    result.append("Model: " + android.os.Build.MODEL + "\n");
    result.append("Build: " + android.os.Build.DISPLAY + "\n");

    if (cfg.netType == ConnectivityManager.TYPE_MOBILE) {
        result.append("Active interface: mobile\n");
    } else if (cfg.netType == ConnectivityManager.TYPE_WIFI) {
        result.append("Active interface: wifi\n");
    } else {/*w ww  .  j a va2  s. c  o m*/
        result.append("Active interface: unknown\n");
    }
    result.append(
            "Tether status: " + (cfg.tetherStatusKnown ? (cfg.isTethered ? "yes" : "no") : "unknown") + "\n");
    result.append("Roam status: " + (cfg.isRoaming ? "yes" : "no") + "\n");
    result.append("IPv4 subnet: " + cfg.lanMaskV4 + "\n");
    result.append("IPv6 subnet: " + cfg.lanMaskV6 + "\n");

    // filesystem calls can block, so run in another thread
    new AsyncTask<Void, Void, String>() {
        @Override
        public String doInBackground(Void... args) {
            StringBuilder ret = new StringBuilder();

            ret.append(getFileInfo("/system/bin/su"));
            ret.append(getFileInfo("/system/xbin/su"));
            ret.append(getFileInfo("/system/app/Superuser.apk"));

            PackageManager pm = ctx.getPackageManager();
            ret.append("Superuser: " + getSuInfo(pm));
            ret.append("\n");

            return ret.toString();
        }

        @Override
        public void onPostExecute(String suInfo) {
            result.append(suInfo);
            appendPreferences(ctx);
        }
    }.execute();

}

From source file:org.lol.reddit.common.General.java

public static boolean isConnectionWifi(final Context context) {
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return info != null && info.isConnected();
}

From source file:com.ryan.ryanreader.common.General.java

/**
 * /*w  w w . j a  va  2s  .  c om*/
 * ?WIFI
 * 
 * @param context
 * @return
 */
public static boolean isConnectionWifi(final Context context) {
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    final NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    return info != null && info.isConnected();
}

From source file:net.mm2d.dmsexplorer.ServerListActivity.java

private boolean hasAvailableNetwork() {
    final NetworkInfo ni = mConnectivityManager.getActiveNetworkInfo();
    return ni != null && ni.isConnected() && (ni.getType() == ConnectivityManager.TYPE_WIFI
            || ni.getType() == ConnectivityManager.TYPE_ETHERNET);
}

From source file:com.kentph.ttcnextbus.CreateDatabaseActivity.java

private void updateConnectedFlags() {
    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
    if (activeInfo != null && activeInfo.isConnected()) {
        wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
        mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
    } else {/*from  w ww .  j av a  2s.  co  m*/
        wifiConnected = false;
        mobileConnected = false;
    }
}

From source file:com.onesignal.OSUtils.java

Integer getNetType() {
    ConnectivityManager cm = (ConnectivityManager) OneSignal.appContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    if (netInfo != null) {
        int networkType = netInfo.getType();
        if (networkType == ConnectivityManager.TYPE_WIFI || networkType == ConnectivityManager.TYPE_ETHERNET)
            return 0;
        return 1;
    }/* w  w w  .ja v  a 2s .  c  om*/

    return null;
}

From source file:it.evilsocket.dsploit.net.Network.java

public static boolean isWifiConnected(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    return info != null && info.isConnected() && info.isAvailable();
}