Example usage for android.net ConnectivityManager TYPE_MOBILE

List of usage examples for android.net ConnectivityManager TYPE_MOBILE

Introduction

In this page you can find the example usage for android.net ConnectivityManager TYPE_MOBILE.

Prototype

int TYPE_MOBILE

To view the source code for android.net ConnectivityManager TYPE_MOBILE.

Click Source Link

Document

A Mobile data connection.

Usage

From source file:RhodesService.java

private static boolean hasNetworkEx(boolean checkCell, boolean checkWifi, boolean checkEthernet,
        boolean checkWimax, boolean checkBluetooth, boolean checkAny) {
    if (!Capabilities.NETWORK_STATE_ENABLED) {
        Logger.E(TAG, "HAS_NETWORK: Capability NETWORK_STATE disabled");
        return false;
    }/*w ww. j av a2  s  .  c o m*/

    Context ctx = RhodesService.getContext();
    ConnectivityManager conn = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (conn == null) {
        Logger.E(TAG, "HAS_NETWORK: cannot create ConnectivityManager");
        return false;
    }

    NetworkInfo[] info = conn.getAllNetworkInfo();
    if (info == null) {
        Logger.E(TAG, "HAS_NETWORK: cannot issue getAllNetworkInfo");
        return false;
    }

    //{
    //   Utils.platformLog("NETWORK", "$$$$$$$$$$$$$$$$$$$   Networks ; $$$$$$$$$$$$$$$$$$$$$$");
    //   for (int i = 0, lim = info.length; i < lim; ++i) 
    //   {
    //      int type = info[i].getType();
    //      String name = info[i].getTypeName();
    //      boolean is_connected = info[i].getState() == NetworkInfo.State.CONNECTED;
    //      Utils.platformLog("NETWORK", "        - Name ["+name+"],  type ["+String.valueOf(type)+"], connected ["+String.valueOf(is_connected)+"]");
    //   }
    //}

    for (int i = 0, lim = info.length; i < lim; ++i) {
        boolean is_connected = info[i].getState() == NetworkInfo.State.CONNECTED;
        int type = info[i].getType();
        if (is_connected) {
            if ((type == ConnectivityManager.TYPE_MOBILE) && (checkCell))
                return true;
            if ((type == ConnectivityManager.TYPE_WIFI) && (checkWifi))
                return true;
            if ((type == 6) && (checkWimax))
                return true;
            if ((type == 9) && (checkEthernet))
                return true;
            if ((type == 7) && (checkBluetooth))
                return true;
            if (checkAny)
                return true;
        }
    }

    Logger.I(TAG, "HAS_NETWORK: all networks are disconnected");

    return false;
}

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/*from  ww  w. ja  v a  2 s. 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.master.metehan.filtereagle.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//from  w w  w  . j  a  va 2  s  .  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) {
            sb.append("\r\n");
            while (nis.hasMoreElements()) {
                NetworkInterface ni = nis.nextElement();
                if (ni != null) {
                    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("\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:at.alladin.rmbt.android.util.InformationCollector.java

/** Returns the network that the phone is on (e.g. Wifi, Edge, GPRS, etc). */
public int getNetwork() {
    int result = TelephonyManager.NETWORK_TYPE_UNKNOWN;

    if (connManager != null) {
        final NetworkInfo activeNetworkInfo = connManager.getActiveNetworkInfo();
        if (activeNetworkInfo != null) {
            final int type = activeNetworkInfo.getType();
            switch (type) {
            case ConnectivityManager.TYPE_WIFI:
                result = NETWORK_WIFI;//from   www .  ja va 2 s.  c o  m
                break;

            case ConnectivityManager.TYPE_BLUETOOTH:
                result = NETWORK_BLUETOOTH;
                break;

            case ConnectivityManager.TYPE_ETHERNET:
                result = NETWORK_ETHERNET;
                break;

            case ConnectivityManager.TYPE_MOBILE:
            case ConnectivityManager.TYPE_MOBILE_DUN:
            case ConnectivityManager.TYPE_MOBILE_HIPRI:
            case ConnectivityManager.TYPE_MOBILE_MMS:
            case ConnectivityManager.TYPE_MOBILE_SUPL:
                result = telManager.getNetworkType();
                break;
            }
        }
    }

    /* detect change from wifi to mobile or reverse */
    final int lastNetworkType = this.lastNetworkType.get();
    if (result != TelephonyManager.NETWORK_TYPE_UNKNOWN
            && lastNetworkType != TelephonyManager.NETWORK_TYPE_UNKNOWN) {
        if ((result == ConnectivityManager.TYPE_WIFI && lastNetworkType != ConnectivityManager.TYPE_WIFI)
                || (result != ConnectivityManager.TYPE_WIFI
                        && lastNetworkType == ConnectivityManager.TYPE_WIFI))
            illegalNetworkTypeChangeDetcted.set(true);
    }
    if (result != lastNetworkType) {
        this.lastNetworkType.set(result);
        if (telListener != null)
            telListener.onSignalStrengthsChanged(null);
    }

    return result;
}

From source file:com.moez.QKSMS.mmssms.Transaction.java

private void sendMMS(final byte[] bytesToSend) {
    revokeWifi(true);// w  w w.  j  a v a2s  . com

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

private void sendMMSWiFi(final byte[] bytesToSend) {
    // enable mms connection to mobile data
    mConnMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo.State state = mConnMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS).getState();

    if ((0 == state.compareTo(NetworkInfo.State.CONNECTED)
            || 0 == state.compareTo(NetworkInfo.State.CONNECTING))) {
        sendData(bytesToSend);//from   w w w .  ja va2s.  com
    } else {
        int resultInt = mConnMgr.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "enableMMS");

        if (resultInt == 0) {
            try {
                Utils.ensureRouteToHost(context, settings.getMmsc(), settings.getProxy());
                sendData(bytesToSend);
            } catch (Exception e) {
                Log.e(TAG, "exception thrown", e);
                sendData(bytesToSend);
            }
        } else {
            // 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;
                    }

                    NetworkInfo mNetworkInfo = mConnMgr.getActiveNetworkInfo();
                    if ((mNetworkInfo == null)
                            || (mNetworkInfo.getType() != ConnectivityManager.TYPE_MOBILE_MMS)) {
                        return;
                    }

                    if (!mNetworkInfo.isConnected()) {
                        return;
                    } else {
                        alreadySending = true;

                        try {
                            Utils.ensureRouteToHost(context, settings.getMmsc(), settings.getProxy());
                            sendData(bytesToSend);
                        } catch (Exception e) {
                            Log.e(TAG, "exception thrown", e);
                            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 {
                            context.unregisterReceiver(receiver);
                        } catch (Exception e) {

                        }

                        try {
                            Utils.ensureRouteToHost(context, settings.getMmsc(), settings.getProxy());
                            sendData(bytesToSend);
                        } catch (Exception e) {
                            Log.e(TAG, "exception thrown", e);
                            sendData(bytesToSend);
                        }
                    }
                }
            }, 7000);
        }
    }
}

From source file:org.opensilk.music.artwork.ArtworkRequestManagerImpl.java

boolean isOnline(boolean wifiOnly) {

    boolean state = false;

    /* Wi-Fi connection */
    final NetworkInfo wifiNetwork = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null) {
        state = wifiNetwork.isConnectedOrConnecting();
    }//  ww w  . jav a 2 s. co  m

    // Don't bother checking the rest if we are connected or we have opted out of mobile
    if (wifiOnly || state) {
        return state;
    }

    /* Mobile data connection */
    final NetworkInfo mbobileNetwork = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mbobileNetwork != null) {
        state = mbobileNetwork.isConnectedOrConnecting();
    }

    /* Other networks */
    final NetworkInfo activeNetwork = mConnectivityManager.getActiveNetworkInfo();
    if (activeNetwork != null) {
        state = activeNetwork.isConnectedOrConnecting();
    }

    return state;
}

From source file:cx.ring.service.LocalService.java

private void updateConnectivityState() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    Log.w(TAG, "ActiveNetworkInfo (Wifi): " + (ni == null ? "null" : ni.toString()));
    isWifiConn = ni != null && ni.isConnected();

    ni = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    Log.w(TAG, "ActiveNetworkInfo (mobile): " + (ni == null ? "null" : ni.toString()));
    isMobileConn = ni != null && ni.isConnected();

    try {//from w  w  w  .  ja  v a2s.  co  m
        getRemoteService().setAccountsActive(isConnected());
    } catch (RemoteException e) {
        e.printStackTrace();
    }

    // if account list loaded
    if (!ip2ip_account.isEmpty())
        sendBroadcast(new Intent(ACTION_ACCOUNT_UPDATE));
}

From source file:uk.co.senab.photoview.sample.ViewPagerActivity.java

public static boolean internetIsConnected(final Activity activity) {

    boolean connected = false;

    // Get connect mangaer
    final ConnectivityManager connMgr = (ConnectivityManager) activity
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    // check for wifi
    final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    // check for mobile data
    final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (wifi.isAvailable()) {

        connected = true;// w  w  w  . j ava  2  s. co  m

    } else if (mobile.isAvailable()) {

        connected = true;

    } else {

        connected = false;
    }

    return connected;

}