Example usage for android.net NetworkInfo getType

List of usage examples for android.net NetworkInfo getType

Introduction

In this page you can find the example usage for android.net NetworkInfo getType.

Prototype

@Deprecated
public int getType() 

Source Link

Document

Reports the type of network to which the info in this NetworkInfo pertains.

Usage

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 va 2s.  c  om
    }

    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.zegoggles.smssync.service.ServiceBase.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private boolean isConnectedViaWifi_SDK21() {
    for (Network network : getConnectivityManager().getAllNetworks()) {
        final NetworkInfo networkInfo = getConnectivityManager().getNetworkInfo(network);
        if (networkInfo != null && networkInfo.getType() == TYPE_WIFI
                && networkInfo.isConnectedOrConnecting()) {
            return true;
        }//from w ww.  java 2 s  .c o  m
    }
    return false;
}

From source file:com.owncloud.activity.Downloader.java

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        // Try This
        int i = netInfo.getType();
        System.out.println("Net Type =" + i);

        return true;
    }/* w ww. j a v a 2  s.c om*/
    return false;

}

From source file:fr.gcastel.freeboxV6GeekIncDownloader.services.FreeboxDownloaderService.java

public boolean isConnectedViaWifi() {
    boolean result = false;
    if (zeContext != null) {
        ConnectivityManager cm = (ConnectivityManager) zeContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        result = (info != null) && (info.getType() == ConnectivityManager.TYPE_WIFI);
    }//  w  ww. j  a  v  a 2  s  . com
    return result;
}

From source file:com.github.marcosalis.kraken.utils.network.NetworkBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {

    logNetworkInfo(intent);//from  w w w .ja  v a  2s.  com

    // use EXTRA_NO_CONNECTIVITY to check if there is no connection
    if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) {
        notifyConnectionGone(context);
        return;
    }

    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo netInfo = cm.getActiveNetworkInfo();

    // send local broadcast to notify all registered receivers
    if (netInfo != null && netInfo.isConnected()) { // connection active
        notifyConnectionActive(context, netInfo.getType());
    } else {
        // connection has gone
        notifyConnectionGone(context);
    }
}

From source file:com.github.luluvise.droid_utils.lib.network.NetworkBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {

    if (DroidConfig.DEBUG) { // debugging network info
        final NetworkInfo otherNetworkInfo = (NetworkInfo) intent
                .getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
        final String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
        final boolean failover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
        Log.i(TAG,//from w  w w .  ja va 2 s  . c  om
                "onReceive(): " + " otherNetworkInfo = "
                        + (otherNetworkInfo == null ? "[none]" : otherNetworkInfo) + ", failover=" + failover
                        + ", reason=" + reason);
    }

    // use EXTRA_NO_CONNECTIVITY to check if there is no connection
    if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) {
        notifyConnectionGone(context);
        return;
    }

    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo netInfo = cm.getActiveNetworkInfo();

    // send local broadcast to notify all registered receivers
    if (netInfo != null && netInfo.isConnected()) { // connection active
        notifyConnectionActive(context, netInfo.getType());
    } else {
        // connection has gone
        notifyConnectionGone(context);
    }
}

From source file:github.daneren2005.dsub.util.Util.java

public static int getPreloadCount(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    if (networkInfo == null) {
        return 3;
    }// w  w  w  .j av  a  2 s .c o m

    SharedPreferences prefs = getPreferences(context);
    boolean wifi = networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
    int preloadCount = Integer.parseInt(prefs.getString(wifi ? Constants.PREFERENCES_KEY_PRELOAD_COUNT_WIFI
            : Constants.PREFERENCES_KEY_PRELOAD_COUNT_MOBILE, "-1"));
    return preloadCount == -1 ? Integer.MAX_VALUE : preloadCount;
}

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

@Override
protected void onHandleIntent(Intent intent) {
    try {/*from  w w w .java  2  s  .c  o  m*/
        // 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);
    }
}

From source file:android_network.hetnet.vpn_service.Util.java

public static String getNetworkInfo(Context context) {
    StringBuilder sb = new StringBuilder();
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo ani = cm.getActiveNetworkInfo();
    List<NetworkInfo> listNI = new ArrayList<>();

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
        listNI.addAll(Arrays.asList(cm.getAllNetworkInfo()));
    else// w  w w  .  j av a2s  . c o m
        for (Network network : cm.getAllNetworks()) {
            NetworkInfo ni = cm.getNetworkInfo(network);
            if (ni != null)
                listNI.add(ni);
        }

    for (NetworkInfo ni : listNI) {
        sb.append(ni.getTypeName()).append('/').append(ni.getSubtypeName()).append(' ')
                .append(ni.getDetailedState())
                .append(TextUtils.isEmpty(ni.getExtraInfo()) ? "" : " " + ni.getExtraInfo())
                .append(ni.getType() == ConnectivityManager.TYPE_MOBILE
                        ? " " + Util.getNetworkGeneration(ni.getSubtype())
                        : "")
                .append(ni.isRoaming() ? " R" : "")
                .append(ani != null && ni.getType() == ani.getType() && ni.getSubtype() == ani.getSubtype()
                        ? " *"
                        : "")
                .append("\r\n");
    }

    try {
        Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
        if (nis != null)
            while (nis.hasMoreElements()) {
                NetworkInterface ni = nis.nextElement();
                if (ni != null && !ni.isLoopback()) {
                    List<InterfaceAddress> ias = ni.getInterfaceAddresses();
                    if (ias != null)
                        for (InterfaceAddress ia : ias)
                            sb.append(ni.getName()).append(' ').append(ia.getAddress().getHostAddress())
                                    .append('/').append(ia.getNetworkPrefixLength()).append(' ')
                                    .append(ni.getMTU()).append(' ').append(ni.isUp() ? '^' : 'v')
                                    .append("\r\n");
                }
            }
    } catch (Throwable ex) {
        sb.append(ex.toString()).append("\r\n");
    }

    if (sb.length() > 2)
        sb.setLength(sb.length() - 2);

    return sb.toString();
}

From source file:com.OpenSource.engine.connectivity.ConnectivityInfoProvider.java

public boolean isWiFiConnected() {
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo == null) {
        return false;
    }/*www .j ava2  s  .  c om*/
    return networkInfo.getState() == NetworkInfo.State.CONNECTED
            && networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
}