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:net.helff.wificonnector.WifiConnectivityReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    Log.i(TAG, "action: " + intent.getAction());

    if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {

        Intent locationIntent = new LocationIntent();
        context.sendBroadcast(locationIntent);
    }/*from  w w  w .  jav a2 s . com*/

    if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {

        NetworkInfo networkInfo = (NetworkInfo) intent
                .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

        Log.d(TAG, "got network_state_changed with detailed info: " + networkInfo == null ? "nothing"
                : networkInfo.getDetailedState().name());

        if (networkInfo != null && networkInfo.isConnected()
                && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
            boolean autoConnect = prefs.getBoolean("autoConnect", false);

            Log.d(TAG, "triggering WifiConnectivityService with autoConnect=" + autoConnect);

            int command = autoConnect ? WifiConnectivityService.COMMAND_AUTO_UNLOCK_CONNECTION
                    : WifiConnectivityService.COMMAND_CHECK_CONNECTION;
            Intent msgIntent = new Intent(context, WifiConnectivityService.class);
            msgIntent.putExtra(WifiConnectivityService.INTENT_COMMAND, command);
            context.startService(msgIntent);
        }

    }
    if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
        Log.d(TAG, "triggering WifiConnectivityService state changed");

        Intent msgIntent = new Intent(context, WifiConnectivityService.class);
        msgIntent.putExtra(WifiConnectivityService.INTENT_COMMAND,
                WifiConnectivityService.COMMAND_REFRESH_STATUS);
        context.startService(msgIntent);
    }
}

From source file:com.mytwitter.Network.NetworkHelper.java

public static boolean connectedToWiFiNetwork(ConnectivityManager connectivityManager) {
    NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();

    if (!isConnected)
        return false;

    return (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI);
}

From source file:Main.java

public static int getConnectedType(Context context) {
    ConnectivityManager cManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo nInfo = cManager.getActiveNetworkInfo();
    if (nInfo != null && nInfo.isAvailable()) {
        int type = nInfo.getType();
        int subType = nInfo.getSubtype();
        switch (type) {
        case ConnectivityManager.TYPE_MOBILE:
            switch (subType) {
            case 1://TelephonyManager.NETWORK_TYPE_GPRS:
            case 2://TelephonyManager.NETWORK_TYPE_EDGE:
            case 4://TelephonyManager.NETWORK_TYPE_CDMA:
            case 7://TelephonyManager.NETWORK_TYPE_1xRTT:
            case 11://TelephonyManager.NETWORK_TYPE_IDEN:
                return NETWORK_CLASS_2_G;
            case 3://TelephonyManager.NETWORK_TYPE_UMTS:
            case 5://TelephonyManager.NETWORK_TYPE_EVDO_0:
            case 6://TelephonyManager.NETWORK_TYPE_EVDO_A:
            case 8://TelephonyManager.NETWORK_TYPE_HSDPA:
            case 9://TelephonyManager.NETWORK_TYPE_HSUPA:
            case 10://TelephonyManager.NETWORK_TYPE_HSPA:
            case 12://TelephonyManager.NETWORK_TYPE_EVDO_B:
            case 14://TelephonyManager.NETWORK_TYPE_EHRPD:
            case 15://TelephonyManager.NETWORK_TYPE_HSPAP:
                return NETWORK_CLASS_3_G;
            case 13://TelephonyManager.NETWORK_TYPE_LTE:
                return NETWORK_CLASS_4_G;
            default:
                return NETWORK_CLASS_UNKNOWN;
            }//from   w ww. jav a 2s  .c  om
        case ConnectivityManager.TYPE_WIFI:

            return NETWORK_CLASS_WIFI;
        }
    }
    return NETWORK_CLASS_UNKNOWN;
}

From source file:com.nextgis.maplib.util.NetworkUtil.java

public boolean isNetworkAvailable() {
    if (mConnectionManager == null) {
        return false;
    }/*from   w  w w . j ava 2  s  . c o m*/

    NetworkInfo info = mConnectionManager.getActiveNetworkInfo();
    if (info == null) //|| !cm.getBackgroundDataSetting()
    {
        return false;
    }

    int netType = info.getType();
    if (netType == ConnectivityManager.TYPE_WIFI) {
        return info.isConnected();
    } else if (netType == ConnectivityManager.TYPE_MOBILE) { // netSubtype == TelephonyManager.NETWORK_TYPE_UMTS
        if (mTelephonyManager != null && !mTelephonyManager.isNetworkRoaming()) {
            return info.isConnected();
        }
    }

    return false;
}

From source file:com.tomeokin.archsample.data.remote.NetworkReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    SharedPreferences preferences = ArchApplication.getApp()
            .getSharedPreferences(CollectManager.PREF_COLLECTION, Context.MODE_PRIVATE);
    final boolean haveCollection = preferences.getBoolean(CollectManager.HAVE_NEW_COLLECTIONS, false);
    final int count = preferences.getInt(CollectManager.NEW_COLLECTION_COUNT, 0);
    if (!haveCollection || count <= 0) {
        return;//ww  w.j  a  v  a 2  s .  com
    }

    Logger.t("NetworkReceiver").i("connection state change");
    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifi.isConnected()) {
            Logger.t("NetworkReceiver").i("wifi is connected");
            //List<Collection> collections = CollectManager.getInstance()
            //    .getCollectionsFromPref();

            PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class),
                    0);
            Notification notification = new NotificationCompat.Builder(context)
                    .setTicker("have new collections").setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(count + " new collections")
                    //.setContentText("have new collections")
                    .setContentIntent(pi).setAutoCancel(true).build();
            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(ACTION_SHOW_NOTIFICATION_REQUEST_CODE, notification);
            preferences.edit().putBoolean(CollectManager.HAVE_NEW_COLLECTIONS, false).apply();
        }
    }
}

From source file:fr.mixit.android.utils.NetworkUtils.java

public static ConnectivityState getConnectivity(Context ctx) {
    ConnectivityState currentNetworkType;
    final ConnectivityManager conMgr = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable()
            && conMgr.getActiveNetworkInfo().isConnected()) {
        NetworkInfo networkInfo = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (networkInfo != null && networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
            currentNetworkType = ConnectivityState.WIFI;
        } else {/* w  w  w.  j  a  v a2  s. c  o m*/
            networkInfo = conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (networkInfo != null && networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
                currentNetworkType = ConnectivityState.CARRIER;
            } else {
                // how can we be there ? no wifi and no mobile data but connected ?
                currentNetworkType = ConnectivityState.UNKNOWN;
            }
        }
    } else {
        currentNetworkType = ConnectivityState.NONE;
    }

    return currentNetworkType;
}

From source file:com.frostwire.android.gui.NetworkManager.java

/**
 * aka -> isInternetUp//  www  . j a va  2  s .  c  o m
 */
public boolean isDataUp() {
    ConnectivityManager connectivityManager = getConnectivityManager();

    boolean wifiUp = isNetworkTypeUp(connectivityManager, ConnectivityManager.TYPE_WIFI);
    boolean mobileUp = isNetworkTypeUp(connectivityManager, ConnectivityManager.TYPE_MOBILE);

    // boolean logic trick, since sometimes android reports WIFI and MOBILE up at the same time
    return wifiUp != mobileUp;
}

From source file:org.basdroid.common.NetworkUtils.java

/**
 * Check if the connection is fast//from  www.  j ava  2  s .  c  om
 * @param type
 * @param subType
 * @return
 */
public static boolean isConnectionFast(int type, int subType) {
    if (type == ConnectivityManager.TYPE_WIFI) {
        return true;
    } else if (type == ConnectivityManager.TYPE_MOBILE) {
        switch (subType) {
        case TelephonyManager.NETWORK_TYPE_1xRTT:
            return false; // ~ 50-100 kbps
        case TelephonyManager.NETWORK_TYPE_CDMA:
            return false; // ~ 14-64 kbps
        case TelephonyManager.NETWORK_TYPE_EDGE:
            return false; // ~ 50-100 kbps
        case TelephonyManager.NETWORK_TYPE_EVDO_0:
            return true; // ~ 400-1000 kbps
        case TelephonyManager.NETWORK_TYPE_EVDO_A:
            return true; // ~ 600-1400 kbps
        case TelephonyManager.NETWORK_TYPE_GPRS:
            return false; // ~ 100 kbps
        case TelephonyManager.NETWORK_TYPE_HSDPA:
            return true; // ~ 2-14 Mbps
        case TelephonyManager.NETWORK_TYPE_HSPA:
            return true; // ~ 700-1700 kbps
        case TelephonyManager.NETWORK_TYPE_HSUPA:
            return true; // ~ 1-23 Mbps
        case TelephonyManager.NETWORK_TYPE_UMTS:
            return true; // ~ 400-7000 kbps
        /*
         * Above API level 7, make sure to set android:targetSdkVersion
         * to appropriate level to use these
         */
        case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
            return true; // ~ 1-2 Mbps
        case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
            return true; // ~ 5 Mbps
        case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
            return true; // ~ 10-20 Mbps
        case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
            return false; // ~25 kbps
        case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
            return true; // ~ 10+ Mbps
        // Unknown
        case TelephonyManager.NETWORK_TYPE_UNKNOWN:
        default:
            return false;
        }
    } else {
        return false;
    }
}

From source file:com.near.chimerarevo.services.NewsService.java

@Override
protected void onHandleIntent(Intent i) {
    ConnectivityManager mConnManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    if (mConnManager != null) {
        mWifi = mConnManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        mMobile = mConnManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (!isMobileConnected() && !isWiFiConnected())
            return;
    }//from w  w w .  ja  v  a  2s .c  o  m

    if (i.getBooleanExtra("shouldNotCreateNotification", false))
        mHandler.post(new DisplayToast(this, getResources().getString(R.string.text_loading)));

    Request request = new Request.Builder().url(URLUtils.getUrl()).build();

    try {
        Response response = OkHttpUtils.getInstance().newCall(request).execute();

        String body = response.body().string().trim();

        if (!body.isEmpty())
            if (readOfflineFile(body)) {
                ArrayList<String> mJson = new ArrayList<>();
                mJson.add((new JsonParser()).parse(body).toString());
                SysUtils.writeOfflineFile(this, mJson, "posts.ser");

                Intent update = new Intent(this, PostsListWidgetProvider.class);
                update.setAction(PostsListWidgetProvider.REFRESH_VIEWS_ACTION);
                sendBroadcast(update);

                update = new Intent(this, PostsLRWidgetProvider.class);
                update.setAction(PostsLRWidgetProvider.REFRESH_VIEWS_ACTION);
                sendBroadcast(update);

                if (!i.getBooleanExtra("shouldNotCreateNotification", false))
                    createNotification();
            }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.zionsoft.obadiah.model.utils.AppUpdateChecker.java

@Override
protected void onHandleIntent(Intent intent) {
    try {//  www.  jav a  2  s. c om
        // we only check if at least 24 hours is passed
        final SharedPreferences preferences = getSharedPreferences(Constants.PREF_NAME, Context.MODE_PRIVATE);
        final long now = System.currentTimeMillis();
        final long lastCheckedTimestamp = preferences
                .getLong(Constants.PREF_KEY_CHECKED_APPLICATION_VERSION_TIMESTAMP, 0);
        if (now - lastCheckedTimestamp < DateUtils.DAY_IN_MILLIS) {
            return;
        }

        // we only check if the user has active WiFi or WiMAX
        final NetworkInfo networkInfo = ((ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE))
                .getActiveNetworkInfo();
        if (networkInfo == null || !networkInfo.isConnected()) {
            return;
        }
        final int networkType = networkInfo.getType();
        if (networkType != ConnectivityManager.TYPE_WIFI && networkType != ConnectivityManager.TYPE_WIMAX) {
            return;
        }

        final String response = new String(NetworkHelper.get(NetworkHelper.CLIENT_VERSION_URL), "UTF-8");
        final JSONObject versionObject = new JSONObject(response);
        final int latestVersion = versionObject.getInt("versionCode");
        final SharedPreferences.Editor editor = preferences.edit();
        if (latestVersion < preferences.getInt(Constants.PREF_KEY_CHECKED_APPLICATION_VERSION, 0)) {
            editor.putInt(Constants.PREF_KEY_CHECKED_APPLICATION_VERSION, latestVersion)
                    .putBoolean(Constants.PREF_KEY_ASKED_APPLICATION_UPDATE, false);
        }

        editor.putLong(Constants.PREF_KEY_CHECKED_APPLICATION_VERSION_TIMESTAMP, now).apply();
    } catch (Exception e) {
        Crashlytics.logException(e);
    }
}