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:Main.java

public boolean isNetworkRoaming(Context mCm) {
    ConnectivityManager connectivity = (ConnectivityManager) mCm.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        return false;
    }//from www  .j av  a 2  s. c o m
    NetworkInfo info = connectivity.getActiveNetworkInfo();
    boolean isMobile = (info != null && info.getType() == ConnectivityManager.TYPE_MOBILE);
    TelephonyManager mTm = (TelephonyManager) mCm.getSystemService(Context.TELEPHONY_SERVICE);
    boolean isRoaming = isMobile && mTm.isNetworkRoaming();
    return isRoaming;
}

From source file:Main.java

public static int getNetWorkType(Context context) {
    // showLog("getNetWorkType");
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (null == networkInfo || !networkInfo.isAvailable()) {
        return 1;
    }//from   w  w w.  j a v  a 2  s. c o m
    if (State.CONNECTED == connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState()) {
        return 2;
    }
    if (State.CONNECTED == connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState()) {
        return 3;
    }
    return 4;
}

From source file:Main.java

/**
 * Used to determine if there is an active data connection and what type of
 * connection it is if there is one//from w  w  w . ja v  a2 s  .  c  o m
 * 
 * @param context The {@link Context} to use
 * @return True if there is an active data connection, false otherwise.
 *         Also, if the user has checked to only download via Wi-Fi in the
 *         settings, the mobile data and other network connections aren't
 *         returned at all
 */
public static final boolean isOnline(final Context context) {
    /*
     * This sort of handles a sudden configuration change, but I think it
     * should be dealt with in a more professional way.
     */
    if (context == null) {
        return false;
    }

    boolean state = false;

    /* Monitor network connections */
    final ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    /* Wi-Fi connection */
    final NetworkInfo wifiNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null) {
        state = wifiNetwork.isConnectedOrConnecting();
    }

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

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

    return state;
}

From source file:cw.kop.autobackground.files.FileHandler.java

public static boolean download(Context appContext) {

    if (!isDownloading) {
        ConnectivityManager connect = (ConnectivityManager) appContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo wifi = connect.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = connect.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi != null && wifi.isConnected() && AppSettings.useWifi()) {
        } else if (mobile != null && mobile.isConnected() && AppSettings.useMobile()) {
        } else {/*from  w w w.  j  a  va  2s  .  c o m*/
            if (AppSettings.useToast()) {
                Toast.makeText(appContext, "No connection available,\ncheck Download Settings",
                        Toast.LENGTH_SHORT).show();
            }

            Intent resetDownloadIntent = new Intent(FileHandler.DOWNLOAD_TERMINATED);
            LocalBroadcastManager.getInstance(appContext).sendBroadcast(resetDownloadIntent);
            return false;
        }
        isDownloading = true;
        downloadThread = new DownloadThread(appContext);
        downloadThread.start();
        if (AppSettings.useToast()) {
            Toast.makeText(appContext, "Downloading images", Toast.LENGTH_SHORT).show();
        }
        return true;
    } else {
        return false;
    }
}

From source file:edu.mecc.race2ged.helpers.Utils.java

/**
 * Are we connected to WiFi, Mobile Data, or nothing?
 * @return The devices current network state.
 *//*from ww w.  j av a2  s. c om*/
public static int getNetworkStatus(Context context) {
    final ConnectivityManager connMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (wifi != null && wifi.isAvailable() && wifi.isConnected())
        return WIFI;
    else if (mobile != null && mobile.isAvailable() && mobile.isConnected())
        return MOBILE_DATA;
    return NO_CONNECTION;
}

From source file:uk.ac.ucl.excites.sapelli.shared.util.android.DeviceControl.java

/**
 * Check if the device is connected to the Internet via mobile/cellular data (2G/3G/4G).
 * //from www  .  j av a2 s .  c  o m
 * @param cntext
 * @return
 */
public static boolean isMobileOnline(Context context) {
    return isOnline(context, ConnectivityManager.TYPE_MOBILE);
}

From source file:net.primeranks.fs_viewer.fs_replay.ConnectionChangedBroadcastReceiver.java

public void onReceive(Context context, Intent intent) {
    String info = intent.getStringExtra(ConnectivityManager.EXTRA_EXTRA_INFO);
    NetworkInfo nwInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
    Log.d(Config.LOG_AS, info + ": " + nwInfo.getReason());

    HttpParams httpParams = EntryPointActivity.getHttpClient().getParams();
    if (nwInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
        String proxyHost = Proxy.getHost(context);
        if (proxyHost == null) {
            proxyHost = Proxy.getDefaultHost();
        }/*from ww w  . j  a va2s . co m*/
        int proxyPort = Proxy.getPort(context);
        if (proxyPort == -1) {
            proxyPort = Proxy.getDefaultPort();
        }
        if (proxyHost != null && proxyPort > -1) {
            HttpHost proxy = new HttpHost(proxyHost, proxyPort);
            httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        } else {
            httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, null);
        }
    } else {
        httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, null);
    }
}

From source file:com.air.mobilebrowser.NetworkUtil.java

/**
 * Check the current status of internet connectivity.
 * This method iterates over the available network interfaces and
 * checks for an active connection.//from   w w  w. j  a va  2s  . com
 * @return true if a connection was detected, false otherwise.
 */
public static boolean haveInternetConnection(Context context) {
    if (context != null) {
        ConnectivityManager connectivityMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo wifiInfo = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wimax = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIMAX);
        NetworkInfo blue = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_BLUETOOTH);
        NetworkInfo ether = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);

        boolean hasInternet = false;

        if (wifiInfo != null && wifiInfo.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        } else if (mobile != null && mobile.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        } else if (wimax != null && wimax.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        } else if (blue != null && blue.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        } else if (ether != null && ether.getState() == NetworkInfo.State.CONNECTED) {
            hasInternet = true;
        }

        return hasInternet;
    }

    return false;
}

From source file:com.ppshein.sevendaynews.common.java

public static Boolean isNetAvailable(Context con) {
    try {//from  w  ww. j  av a  2  s .  c om
        ConnectivityManager connectivityManager = (ConnectivityManager) con
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (wifiInfo.isConnected() || mobileInfo.isConnected()) {
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:it.unicaradio.android.utils.NetworkUtils.java

public static boolean isConnectedToMobileData(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (info == null) {
        return false;
    }/*from  w  ww .  ja v  a  2 s  .  com*/

    return info.isConnected();
}