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:com.moez.QKSMS.mmssms.Transaction.java

private void sendMMS(final byte[] bytesToSend) {
    revokeWifi(true);//from w  ww . j av  a  2 s .  co m

    // enable mms connection to mobile data
    mConnMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    int result = beginMmsConnectivity();

    if (LOCAL_LOGV)
        Log.v(TAG, "result of connectivity: " + result + " ");

    if (result != 0) {
        // if mms feature is not already running (most likely isn't...) then register a receiver and wait for it to be active
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        final BroadcastReceiver receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context1, Intent intent) {
                String action = intent.getAction();

                if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                    return;
                }

                @SuppressWarnings("deprecation")
                NetworkInfo mNetworkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

                if ((mNetworkInfo == null) || (mNetworkInfo.getType() != ConnectivityManager.TYPE_MOBILE)) {
                    return;
                }

                if (!mNetworkInfo.isConnected()) {
                    return;
                } else {
                    // ready to send the message now
                    if (LOCAL_LOGV)
                        Log.v(TAG, "sending through broadcast receiver");
                    alreadySending = true;
                    sendData(bytesToSend);

                    context.unregisterReceiver(this);
                }

            }

        };

        context.registerReceiver(receiver, filter);

        try {
            Looper.prepare();
        } catch (Exception e) {
            // Already on UI thread probably
        }

        // try sending after 3 seconds anyways if for some reason the receiver doesn't work
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!alreadySending) {
                    try {
                        if (LOCAL_LOGV)
                            Log.v(TAG, "sending through handler");
                        context.unregisterReceiver(receiver);
                    } catch (Exception e) {

                    }

                    sendData(bytesToSend);
                }
            }
        }, 7000);
    } else {
        // mms connection already active, so send the message
        if (LOCAL_LOGV)
            Log.v(TAG, "sending right away, already ready");
        sendData(bytesToSend);
    }
}

From source file:nl.privacybarometer.privacyvandaag.service.FetcherService.java

@Override
public void onHandleIntent(Intent intent) {
    if (intent == null) { // No intent, we quit
        return;// w  w  w . j av a  2 s.  c  om
    }

    boolean isFromAutoRefresh = intent.getBooleanExtra(Constants.FROM_AUTO_REFRESH, false);

    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(
            Context.CONNECTIVITY_SERVICE);
    final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    // Connectivity issue, we quit
    if (networkInfo == null || networkInfo.getState() != NetworkInfo.State.CONNECTED) {
        if (ACTION_REFRESH_FEEDS.equals(intent.getAction()) && !isFromAutoRefresh) {
            // Display a toast in that case
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(FetcherService.this, R.string.network_error, Toast.LENGTH_SHORT).show();
                }
            });
        }
        return;
    }

    boolean skipFetch = isFromAutoRefresh && PrefUtils.getBoolean(PrefUtils.REFRESH_WIFI_ONLY, false)
            && networkInfo.getType() != ConnectivityManager.TYPE_WIFI;
    // We need to skip the fetching process, so we quit
    if (skipFetch) {
        return;
    }

    if (ACTION_MOBILIZE_FEEDS.equals(intent.getAction())) {
        mobilizeAllEntries();
        downloadAllImages();
    } else if (ACTION_DOWNLOAD_IMAGES.equals(intent.getAction())) {
        downloadAllImages();
    } else { // == Constants.ACTION_REFRESH_FEEDS
        PrefUtils.putBoolean(PrefUtils.IS_REFRESHING, true);

        if (isFromAutoRefresh) {
            PrefUtils.putLong(PrefUtils.LAST_SCHEDULED_REFRESH, SystemClock.elapsedRealtime());
        }

        /* ModPrivacyVandaag: De defaultwaarde voor het bewaren van artikelen is 30.
        Dus moet de defaultwaarde bij het ophalen ook die waarde hebben, om te voorkomen dat de
        voorkeuren (nog) niet goed ingesteld of gelezen worden.
        In onderstaande dus bij de getString "30" geplaatst. Origineel was "4"
         */
        long keepTime = Long.parseLong(PrefUtils.getString(PrefUtils.KEEP_TIME, "30")) * 86400000l;
        long keepDateBorderTime = keepTime > 0 ? System.currentTimeMillis() - keepTime : 0;

        deleteOldEntries(keepDateBorderTime);

        String feedId = intent.getStringExtra(Constants.FEED_ID);
        int newCount = (feedId == null ? refreshFeeds(keepDateBorderTime)
                : refreshFeed(feedId, keepDateBorderTime)); // number of new articles found after refresh all the feeds

        // notification for new articles.
        if (newCount > 0 && isFromAutoRefresh && PrefUtils.getBoolean(PrefUtils.NOTIFICATIONS_ENABLED, true)) {
            int mNotificationId = 2; // een willekeurige ID, want we doen er nu niks mee
            Intent notificationIntent = new Intent(FetcherService.this, HomeActivity.class);
            // Voorlopig doen we niets met aantallen, want wekt verwarring omdat aantal in de melding kan afwijken van totaal nieuw.
            newCount += PrefUtils.getInt(PrefUtils.NOTIFICATIONS_PREVIOUS_COUNT, 0); // Tel aantal van bestaande melding erbij op
            String text = getResources().getQuantityString(R.plurals.number_of_new_entries, newCount, newCount);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            PendingIntent contentIntent = PendingIntent.getActivity(FetcherService.this, mNotificationId,
                    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(
                    MainApplication.getContext()) //
                            .setContentIntent(contentIntent) // Wat moet er gebeuren als er op de melding geklikt wordt.
                            .setSmallIcon(R.drawable.ic_statusbar_pv) //
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) //
                            .setTicker(text) //
                            .setWhen(System.currentTimeMillis()) // Set het tijdstip van de melding
                            .setAutoCancel(true) // Melding verwijderen als erop geklikt wordt.
                            .setContentTitle(getString(R.string.app_name))
                            .setStyle(new NotificationCompat.BigTextStyle().bigText(text)) // Style: Tekst over meerdere regels
                            .setContentText(text) // Tekst van de melding
                            .setLights(0xffffffff, 0, 0);

            if (PrefUtils.getBoolean(PrefUtils.NOTIFICATIONS_VIBRATE, false)) {
                notifBuilder.setVibrate(new long[] { 0, 1000 });
            }

            String ringtone = PrefUtils.getString(PrefUtils.NOTIFICATIONS_RINGTONE, null);
            if (ringtone != null && ringtone.length() > 0) {
                notifBuilder.setSound(Uri.parse(ringtone));
            }

            if (PrefUtils.getBoolean(PrefUtils.NOTIFICATIONS_LIGHT, false)) {
                notifBuilder.setLights(0xffffffff, 300, 1000);
            }

            NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            mNotifyMgr.notify(mNotificationId, notifBuilder.build());

        }

        PrefUtils.putInt(PrefUtils.NOTIFICATIONS_PREVIOUS_COUNT, newCount);
        mobilizeAllEntries();
        downloadAllImages();

        PrefUtils.putBoolean(PrefUtils.IS_REFRESHING, false);
    }
}

From source file:com.jins_meme.bridge.MainActivity.java

private Connectivity getNetworkConnectivity(Context context) {
    ConnectivityManager connectivityManager;
    connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected()) {
        return Connectivity.fromNetworkType(activeNetwork.getType());
    } else {/*from  ww  w . j  a  v a 2s  .  c om*/
        return Connectivity.OFFLINE;
    }
}

From source file:com.ferdi2005.secondgram.voip.VoIPService.java

private void updateNetworkType() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    lastNetInfo = info;/*from ww  w.ja va2s. c  o m*/
    int type = VoIPController.NET_TYPE_UNKNOWN;
    if (info != null) {
        switch (info.getType()) {
        case ConnectivityManager.TYPE_MOBILE:
            switch (info.getSubtype()) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
                type = VoIPController.NET_TYPE_GPRS;
                break;
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                type = VoIPController.NET_TYPE_EDGE;
                break;
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                type = VoIPController.NET_TYPE_3G;
                break;
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
                type = VoIPController.NET_TYPE_HSPA;
                break;
            case TelephonyManager.NETWORK_TYPE_LTE:
                type = VoIPController.NET_TYPE_LTE;
                break;
            default:
                type = VoIPController.NET_TYPE_OTHER_MOBILE;
                break;
            }
            break;
        case ConnectivityManager.TYPE_WIFI:
            type = VoIPController.NET_TYPE_WIFI;
            break;
        case ConnectivityManager.TYPE_ETHERNET:
            type = VoIPController.NET_TYPE_ETHERNET;
            break;
        }
    }
    if (controller != null) {
        controller.setNetworkType(type);
    }
}

From source file:com.sonetel.ui.dialpad.DialerFragment.java

protected boolean isInternetAvail(Context context) {
    connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = connectivityManager.getActiveNetworkInfo();

    if (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI) {
        Log.d(THIS_FILE, "Wifi state is now " + ni.getState().name());
        if (ni.getState() == NetworkInfo.State.CONNECTED) {
            return true;
        }/*  w ww  . j av  a2 s  .  c o m*/
    }
    if (ni != null && ni.getType() == ConnectivityManager.TYPE_MOBILE) {
        // Any mobile network connected
        if (ni.getState() == NetworkInfo.State.CONNECTED) {
            int subType = ni.getSubtype();

            // 3G (or better)
            if (subType >= TelephonyManager.NETWORK_TYPE_UMTS) {
                return true;
            }

            // GPRS (or unknown)
            if (subType == TelephonyManager.NETWORK_TYPE_GPRS
                    || subType == TelephonyManager.NETWORK_TYPE_UNKNOWN) {
                return true;
            }

            // EDGE
            if (subType == TelephonyManager.NETWORK_TYPE_EDGE) {
                return true;
            }
        }
    }
    if (ni != null && ni.getType() != ConnectivityManager.TYPE_MOBILE
            && ni.getType() != ConnectivityManager.TYPE_WIFI) {
        if (ni.getState() == NetworkInfo.State.CONNECTED) {
            return true;
        }
    }
    return false;

}

From source file:com.android.exchange.ExchangeService.java

/**
 * Determine whether the account is allowed to sync automatically, as opposed to manually, based
 * on whether the "require manual sync when roaming" policy is in force and applicable
 * @param account the account//  w  ww  .j  av  a  2s .  co m
 * @return whether or not the account can sync automatically
 */
/*package*/ static boolean canAutoSync(Account account) {
    ExchangeService exchangeService = INSTANCE;
    if (exchangeService == null) {
        return false;
    }
    NetworkInfo networkInfo = exchangeService.mNetworkInfo;

    // Enforce manual sync only while roaming here
    long policyKey = account.mPolicyKey;
    // Quick exit from this check
    if ((policyKey != 0) && (networkInfo != null)
            && (ConnectivityManager.isNetworkTypeMobile(networkInfo.getType()))) {
        // We'll cache the Policy data here
        Policy policy = account.mPolicy;
        if (policy == null) {
            policy = Policy.restorePolicyWithId(INSTANCE, policyKey);
            account.mPolicy = policy;
        }
        if (policy != null && policy.mRequireManualSyncWhenRoaming && networkInfo.isRoaming()) {
            return false;
        }
    }
    return true;
}

From source file:org.tigase.messenger.phone.pro.service.XMPPService.java

private int getActiveNetworkType() {
    NetworkInfo info = connManager.getActiveNetworkInfo();
    if (info == null)
        return -1;
    if (!info.isConnected())
        return -1;
    return info.getType();
}

From source file:com.halseyburgund.rwframework.core.RWService.java

/**
 * Checks if data connectivity is available, honoring the flag 
 * mOnlyConnectOverWifi to accept only WiFi and not mobile data
 * connections./*from w ww .  j  a v a 2 s .  c  o m*/
 * 
 * @return true if data connectivity is available
 */
public boolean isConnected() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if ((ni != null) && (ni.isConnected())) {
        if ((mOnlyConnectOverWiFi) && (!(ni.getType() == ConnectivityManager.TYPE_WIFI))) {
            return false;
        }
        return true;
    }
    return false;
}

From source file:com.dwdesign.tweetings.util.Utils.java

public static boolean isOnWifi(final Context context) {
    if (context == null)
        return false;
    final ConnectivityManager conn = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo networkInfo = conn.getActiveNetworkInfo();

    return networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI
            && networkInfo.isConnected();
}