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.amazonaws.mobileconnectors.pinpoint.internal.core.system.AndroidConnectivity.java

private void determineAvailability() {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    inAirplaneMode = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
            0) != 0;//ww w . ja v  a  2  s  .  c o m
    log.info("Airplane mode: " + inAirplaneMode);
    final NetworkInfo networkInfo = cm != null ? cm.getActiveNetworkInfo() : null;
    int networkType = 0;
    // default state
    hasWifi = false;
    // when we have connectivity manager, we assume we have some sort of
    // connectivity
    hasMobile = cm != null;
    // can we obtain network info?
    if (networkInfo != null) {
        if (networkInfo.isConnectedOrConnecting()) {
            networkType = networkInfo.getType();

            hasWifi = networkType == ConnectivityManager.TYPE_WIFI
                    || networkType == ConnectivityManager.TYPE_WIMAX;
            hasMobile = networkType == ConnectivityManager.TYPE_MOBILE
                    || networkType == ConnectivityManager.TYPE_MOBILE_DUN
                    || networkType == ConnectivityManager.TYPE_MOBILE_HIPRI
                    || networkType == ConnectivityManager.TYPE_MOBILE_MMS
                    || networkType == ConnectivityManager.TYPE_MOBILE_SUPL;
        } else {
            // if neither connected or connecting then hasMobile defaults
            // need to be changed to false
            hasMobile = false;
        }
    }
    log.info(String.format("Device Connectivity (%s)",
            hasWifi ? "On Wifi" : (hasMobile ? "On Mobile" : "No network connectivity")));
}

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

/**
 * ?//from   w ww . j ava  2s .  c o m
 * @param context
 * @return
 */
public static int getNetworkType(Context context) {
    if (context == null) {
        return -1;
    }
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (info == null) {
        return -1;
    }
    return info.getType();
}

From source file:jfabrix101.lib.helper.NetworkHelper.java

/**
 * Check if the internet connection is through wifi.
 * /*from  ww w .ja  va2  s  . c  om*/
 * @param context
 * @return true if the connection is through wifi
 */
public static boolean isWiFiConnection(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    try {
        //mobile
        //State mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();

        //wifi
        State wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();

        //         if (mobile == State.CONNECTED || mobile == State.CONNECTING) {
        //             //mobile
        //         } else if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
        //             //wifi
        //         }

        if (wifi == State.CONNECTED || wifi == State.CONNECTING)
            return true;
        else
            return false;

    } catch (Exception ex) {
        Logger mLogger = Logger.getInstance(NetworkHelper.class);
        mLogger.error("Exception in checkInternetConnection() : %s", ex.getMessage());
    }

    return false;
}

From source file:com.licubeclub.zionhs.Notices_Parents.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notices_parents);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    listview = (ListView) findViewById(R.id.listView);

    cManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    mobile = cManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    wifi = cManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    SRL = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    SRL.setColorSchemeColors(Color.rgb(231, 76, 60), Color.rgb(46, 204, 113), Color.rgb(41, 128, 185),
            Color.rgb(241, 196, 15));
    SRL.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override//from   w ww . j a v  a 2  s.c  om
        public void onRefresh() {
            networkTask();
        }
    });

    if (mobile.isConnected() || wifi.isConnected()) {
    } else {
        Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.network_connection_warning),
                Toast.LENGTH_LONG);
        finish();
    }
    networkTask();

}

From source file:cc.psdev.heywifi.MainService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("onStartCommand", "called");
    wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    dm = new DBManager(getApplicationContext(), "data", null, DATABASE_VERSION);
    audm = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    actm = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    pref = new SharedPrefSettings(this);
    Thread thread = new Thread(this);
    thread.start();/* ww w. ja  v  a2s. co  m*/

    return START_STICKY;
}

From source file:com.oneguy.recognize.Util.java

public static boolean isNetworkEnable(Context context) {
    ConnectivityManager con = (ConnectivityManager) context.getSystemService(Activity.CONNECTIVITY_SERVICE);
    boolean wifi = con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
    boolean internet = con.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
    return wifi | internet;
}

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

public static boolean connectedToWiFiOrMobileNetwork(Context context) {
    final ConnectivityManager cm = getConnectivityManager(context);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();

    if (!isConnected)
        return false;

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

From source file:de.geeksfactory.opacclient.reminder.SyncAccountService.java

@Override
protected void doWakefulWork(Intent intent) {
    if (BuildConfig.DEBUG)
        Log.i(NAME, "SyncAccountService started");

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);

    if (!sp.getBoolean(SyncAccountAlarmListener.PREF_SYNC_SERVICE, false)) {
        if (BuildConfig.DEBUG)
            Log.i(NAME, "notifications are disabled");
        return;/*from   www  .j a v a2  s.  co  m*/
    }

    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null) {
        if (!sp.getBoolean("notification_service_wifionly", false)
                || networkInfo.getType() == ConnectivityManager.TYPE_WIFI
                || networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
            syncAccounts();
        } else {
            failed = true;
        }
    } else {
        failed = true;
    }

    if (BuildConfig.DEBUG) {
        Log.i(NAME, "SyncAccountService finished " + (failed ? " with errors" : " " + "successfully"));
    }

    long previousPeriod = sp.getLong(SyncAccountAlarmListener.PREF_SYNC_INTERVAL, 0);
    long newPeriod = failed ? AlarmManager.INTERVAL_HOUR : AlarmManager.INTERVAL_HALF_DAY;
    if (previousPeriod != newPeriod) {
        sp.edit().putLong(SyncAccountAlarmListener.PREF_SYNC_INTERVAL, newPeriod).apply();
        WakefulIntentService.cancelAlarms(this);
        WakefulIntentService.scheduleAlarms(SyncAccountAlarmListener.withOnePeriodBeforeStart(), this);
    }
}

From source file:com.seadee.degree.service.NetworkStateReceiver.java

public boolean isWifiConnected() {
    if (connectmanager == null)
        connectmanager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    return connectmanager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
}

From source file:com.rincliu.library.util.RLNetUtil.java

/**
 * @param context//from w w  w  . j av  a2 s  .c o  m
 * @return
 * @throws Exception
 */
public static boolean isWifiDataEnable(Context context) throws Exception {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean isWifiDataEnable = false;
    isWifiDataEnable = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            .isConnectedOrConnecting();
    return isWifiDataEnable;
}