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.mde.potdroid.helpers.WebsiteInteraction.java

public static int getConnectionType(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetworkInfo == null) {
        return 0;
    }/*  ww  w .  j  a  v a  2  s .co  m*/

    if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
        return 1;
    }

    return 2;
}

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

public static boolean checkForNewCfg(Context context) {
    InterfaceDetails newCfg = getInterfaceDetails(context);

    if (currentCfg != null && currentCfg.equals(newCfg)) {
        return false;
    }//from  w w  w .jav  a2 s  . co  m
    currentCfg = newCfg;

    if (!newCfg.netEnabled) {
        Log.i(TAG, "Now assuming NO connection (all interfaces down)");
    } else {
        if (newCfg.netType == ConnectivityManager.TYPE_WIFI) {
            Log.i(TAG, "Now assuming wifi connection");
        } else if (newCfg.netType == ConnectivityManager.TYPE_MOBILE) {
            Log.i(TAG, "Now assuming 3G connection (" + (newCfg.isRoaming ? "roaming, " : "")
                    + (newCfg.isTethered ? "tethered" : "non-tethered") + ")");
        }

        if (!newCfg.lanMaskV4.equals("")) {
            Log.i(TAG, "IPv4 LAN netmask on " + newCfg.wifiName + ": " + newCfg.lanMaskV4);
        }
        if (!newCfg.lanMaskV6.equals("")) {
            Log.i(TAG, "IPv6 LAN netmask on " + newCfg.wifiName + ": " + newCfg.lanMaskV6);
        }
    }
    return true;
}

From source file:org.smilec.smile.student.HttpMsgForStudent.java

private boolean isConnected() {
    boolean r = true;
    ConnectivityManager manager = (ConnectivityManager) main.getSystemService(Context.CONNECTIVITY_SERVICE);
    //boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
    boolean isWifi = false;

    if (manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI) != null)
        isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();

    //Log.d(APP_TAG,"test if the wifi is connected wifi: " + isWifi + ", mobile: " + is3g);

    if (!isWifi) {
        r = false;// w  w  w  .java2s.c o  m
    }

    return r;
}

From source file:org.ttrssreader.utils.Utils.java

/**
 * Only checks the connectivity without regard to the preferences
 *///from  w  w  w .  j a  v  a2s .  com
public static boolean checkConnected(ConnectivityManager cm, boolean onlyWifi, boolean onlyUnmeteredNetwork) {
    if (cm == null)
        return false;

    NetworkInfo info = cm.getActiveNetworkInfo();
    if (info != null && info.isConnected()) {
        if (onlyWifi && info.getType() != ConnectivityManager.TYPE_WIFI) {
            return false;
        }
        if (onlyUnmeteredNetwork) {
            return !isNetworkMetered(cm);
        }
        return true;
    }
    return false;
}

From source file:com.trigger_context.Main_Service.java

@Override
public void onCreate() {
    super.onCreate();
    main_Service = this;
    SharedPreferences users_sp = getSharedPreferences(Main_Service.USERS, MODE_PRIVATE);
    SharedPreferences data_sp = getSharedPreferences(Main_Service.MY_DATA, MODE_PRIVATE);
    conf_macs = new ArrayList<String>(users_sp.getAll().keySet());
    noti(conf_macs.toString(), "");
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getNetworkInfo(android.net.ConnectivityManager.TYPE_WIFI);
    Intent ServiceIntent = new Intent(this, Network_Service.class);

    if (info != null) {
        if (info.isConnected()) {
            Main_Service.wifi = true;
            Network.setWifiOn(true);//  ww w  .j av  a  2s.c  om
            Thread z = new Thread(new Network());
            z.start();
            try {
                z.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // start service
            startService(ServiceIntent);
        }
    }
    users_sp.registerOnSharedPreferenceChangeListener(this);
    data_sp.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            // timeout n username can only be added or modified. not removed
            if (key.equals("username")) {
                username = sharedPreferences.getString("username", "defUsername");
            } else {
                timeout = sharedPreferences.getLong("timeout", 5 * 60) * 1000;
            }

        }
    });
    Log.i(LOG_TAG, "Main_Service-onCreate");
    noti("main serv", "started");
}

From source file:org.ametro.app.ApplicationEx.java

public boolean isAutoUpdateNetworkAvailable() {
    boolean isWifiConnected = mConnectionManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
    boolean isMobileConnected = mConnectionManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
            .isConnected();//w w  w. j av  a2s . com
    boolean isConnected = false;
    if (GlobalSettings.isUpdateOnlyByWifi(this)) {
        isConnected = isWifiConnected;
    } else if (!GlobalSettings.isUpdateByAnyNetwork(this)) {
        isConnected = isWifiConnected || isMobileConnected;
    } else {
        isConnected = isNetworkAvailable();
    }
    return isConnected;
}

From source file:com.cyanogenmod.account.util.CMAccountUtils.java

public static boolean isWifiConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return mWifi != null && mWifi.isConnected();
}

From source file:com.github.notizklotz.derbunddownloader.download.IssueDownloadService.java

private boolean waitForWifiConnection() {
    boolean connected = false;
    if (wifiManager != null) {
        //WIFI_MODE_FULL was not enough on Xperia Tablet Z Android 4.2 to reconnect to the AP if Wifi was enabled but connection
        //was lost
        myWifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "IssueDownloadWifilock");
        myWifiLock.setReferenceCounted(false);
        myWifiLock.acquire();/*from  w ww .j  ava  2s. c  om*/

        //Wait for Wifi coming up
        long firstCheckMillis = System.currentTimeMillis();
        if (!wifiManager.isWifiEnabled()) {
            notifyUser(getText(R.string.download_connection_failed),
                    getText(R.string.download_connection_failed_no_wifi_text), R.drawable.ic_stat_newspaper);
        } else {
            do {
                NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                assert networkInfo != null;
                connected = networkInfo.isConnected();

                if (!connected) {
                    Log.d(LOG_TAG, "Wifi connection is not yet ready. Wait and recheck");

                    if (System.currentTimeMillis() - firstCheckMillis > WIFI_CHECK_MAX_MILLIS) {
                        break;
                    }

                    try {
                        Thread.sleep(WIFI_RECHECK_WAIT_MILLIS);
                    } catch (InterruptedException e) {
                        Log.wtf(LOG_TAG, "Interrupted while waiting for Wifi connection", e);
                    }
                }
            } while (!connected);
        }
    }
    return connected;
}

From source file:ca.mcgill.hs.serv.LogFileUploaderService.java

/**
 * Determines whether or not the phone is able to upload data over the
 * internet.//from   w  w  w .  j av a 2 s. c  o m
 * 
 * @return True if it is possible to upload, false otherwise.
 */
private boolean canUpload() {
    if (connectivityMgr == null || connectivityMgr.getActiveNetworkInfo() == null) {
        return false;
    }
    if (!wifiOnly) {
        if (connectivityMgr.getActiveNetworkInfo().getState() != NetworkInfo.State.CONNECTED) {
            return false;
        }
    } else {
        if (connectivityMgr.getActiveNetworkInfo().getState() != NetworkInfo.State.CONNECTED
                || connectivityMgr.getActiveNetworkInfo().getType() != ConnectivityManager.TYPE_WIFI) {
            return false;
        }
    }
    return true;
}

From source file:org.ttrssreader.utils.Utils.java

public static int getNetworkType(final ConnectivityManager cm) {
    if (cm == null)
        return NETWORK_NONE;
    final NetworkInfo info = cm.getActiveNetworkInfo();
    if (info == null || !info.isConnected()) {
        return NETWORK_NONE;
    } else if (info.getType() != ConnectivityManager.TYPE_WIFI) {
        return NETWORK_MOBILE;
    } else if (isNetworkMetered(cm)) {
        return NETWORK_METERED;
    } else {//www.j  ava 2 s .  c  o  m
        return NETWORK_WIFI;
    }
}