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:org.odk.collect.android.upload.AutoSendWorker.java

/**
 * Returns whether the currently-available connection type is included in the app-level auto-send
 * settings./*  w ww.ja v  a2s  .  c  o m*/
 *
 * @return true if a connection is available and settings specify it should trigger auto-send,
 * false otherwise.
 */
private boolean networkTypeMatchesAutoSendSetting(NetworkInfo currentNetworkInfo) {
    if (currentNetworkInfo == null) {
        return false;
    }

    String autosend = (String) GeneralSharedPreferences.getInstance().get(PreferenceKeys.KEY_AUTOSEND);
    boolean sendwifi = autosend.equals("wifi_only");
    boolean sendnetwork = autosend.equals("cellular_only");
    if (autosend.equals("wifi_and_cellular")) {
        sendwifi = true;
        sendnetwork = true;
    }

    return currentNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI && sendwifi
            || currentNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE && sendnetwork;
}

From source file:fr.univsavoie.ltp.client.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Appliquer le thme LTP a l'ActionBar
    //setTheme(R.style.Theme_ltp);

    // Cration de l'activit principale
    setContentView(R.layout.activity_main);

    // Instancier les classes utiles
    setPopup(new Popup(this));
    setSession(new Session(this));
    setTools(new Tools(this));

    // Afficher la ActionBar
    ActionBar mActionBar = getSupportActionBar();
    mActionBar.setHomeButtonEnabled(true);
    mActionBar.setDisplayShowHomeEnabled(true);

    // MapView settings
    map = (MapView) findViewById(R.id.openmapview);
    map.setTileSource(TileSourceFactory.MAPNIK);
    map.setBuiltInZoomControls(false);/*from   w  ww . j  a v a  2 s  . c  om*/
    map.setMultiTouchControls(true);

    // MapController settings
    mapController = map.getController();

    //To use MapEventsReceiver methods, we add a MapEventsOverlay:
    overlay = new MapEventsOverlay(this, this);
    map.getOverlays().add(overlay);

    boolean isWifiEnabled = false;
    boolean isGPSEnabled = false;

    // Vrifier si le wifi ou le rseau mobile est activ
    final ConnectivityManager connMgr = (ConnectivityManager) this
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    final NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (wifi.isAvailable() && (wifi.getDetailedState() == DetailedState.CONNECTING
            || wifi.getDetailedState() == DetailedState.CONNECTED)) {
        Toast.makeText(this, R.string.toast_wifi, Toast.LENGTH_LONG).show();
        isWifiEnabled = true;
    } else if (mobile.isAvailable() && (mobile.getDetailedState() == DetailedState.CONNECTING
            || mobile.getDetailedState() == DetailedState.CONNECTED)) {
        Toast.makeText(this, R.string.toast_3G, Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this, R.string.toast_aucun_reseau, Toast.LENGTH_LONG).show();
    }

    // Obtenir le service de localisation
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    // Verifier si le service de localisation GPS est actif, le cas echeant, tester le rseau
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 1000, 250.0f, this);
        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        isGPSEnabled = true;
    } else if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30 * 1000, 250.0f, this);
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    }

    // Afficher une boite de dialogue et proposer d'activer un ou plusieurs services pas actifs
    if (!isWifiEnabled || !isGPSEnabled) {
        //getTools().showSettingsAlert(this, isWifiEnabled, isGPSEnabled);
    }

    // Si on a une localisation, on dfinit ses coordonnes geopoint
    if (location != null) {
        startPoint = new GeoPoint(location.getLatitude(), location.getLongitude());
    } else {
        // Sinon, on indique des paramtres par dfaut
        location = getTools().getLastKnownLocation(locationManager);
        if (location == null) {
            location = new Location("");
            location.setLatitude(46.227638);
            location.setLongitude(2.213749000000);
        }
        startPoint = new GeoPoint(46.227638, 2.213749000000);
    }

    setLongitude(location.getLongitude());
    setLatitude(location.getLatitude());

    destinationPoint = null;
    viaPoints = new ArrayList<GeoPoint>();

    // On recupre quelques paramtres de la session prcdents si possible
    if (savedInstanceState == null) {
        mapController.setZoom(15);
        mapController.setCenter(startPoint);
    } else {
        mapController.setZoom(savedInstanceState.getInt("zoom_level"));
        mapController.setCenter((GeoPoint) savedInstanceState.getParcelable("map_center"));
    }

    // Crer un overlay sur la carte pour afficher notre point de dpart
    myLocationOverlay = new SimpleLocationOverlay(this, new DefaultResourceProxyImpl(this));
    map.getOverlays().add(myLocationOverlay);
    myLocationOverlay.setLocation(startPoint);

    // Boutton pour zoomer la carte
    ImageButton btZoomIn = (ImageButton) findViewById(R.id.btZoomIn);
    btZoomIn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            map.getController().zoomIn();
        }
    });

    // Boutton pour dezoomer la carte
    ImageButton btZoomOut = (ImageButton) findViewById(R.id.btZoomOut);
    btZoomOut.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            map.getController().zoomOut();
        }
    });

    // Pointeurs d'itinrairea:
    final ArrayList<ExtendedOverlayItem> waypointsItems = new ArrayList<ExtendedOverlayItem>();
    itineraryMarkers = new ItemizedOverlayWithBubble<ExtendedOverlayItem>(this, waypointsItems, map,
            new ViaPointInfoWindow(R.layout.itinerary_bubble, map));
    map.getOverlays().add(itineraryMarkers);
    //updateUIWithItineraryMarkers();

    Button searchButton = (Button) findViewById(R.id.buttonSearch);
    searchButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            handleSearchLocationButton();
        }
    });

    //context menu for clicking on the map is registered on this button. 
    registerForContextMenu(searchButton);

    // Routes et Itinraires
    final ArrayList<ExtendedOverlayItem> roadItems = new ArrayList<ExtendedOverlayItem>();
    roadNodeMarkers = new ItemizedOverlayWithBubble<ExtendedOverlayItem>(this, roadItems, map);
    map.getOverlays().add(roadNodeMarkers);

    if (savedInstanceState != null) {
        mRoad = savedInstanceState.getParcelable("road");
        updateUIWithRoad(mRoad);
    }

    //POIs:
    //POI search interface:
    String[] poiTags = getResources().getStringArray(R.array.poi_tags);
    poiTagText = (AutoCompleteTextView) findViewById(R.id.poiTag);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,
            poiTags);
    poiTagText.setAdapter(adapter);
    Button setPOITagButton = (Button) findViewById(R.id.buttonSetPOITag);
    setPOITagButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //Hide the soft keyboard:
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(poiTagText.getWindowToken(), 0);
            //Start search:
            getPOIAsync(poiTagText.getText().toString());
        }
    });

    //POI markers:
    final ArrayList<ExtendedOverlayItem> poiItems = new ArrayList<ExtendedOverlayItem>();
    poiMarkers = new ItemizedOverlayWithBubble<ExtendedOverlayItem>(this, poiItems, map,
            new POIInfoWindow(map));
    map.getOverlays().add(poiMarkers);
    if (savedInstanceState != null) {
        mPOIs = savedInstanceState.getParcelableArrayList("poi");
        updateUIWithPOI(mPOIs);
    }

    // Load friends ListView
    lvListeFriends = (ListView) findViewById(R.id.listViewFriends);
    //lvListeFriends.setBackgroundResource(R.drawable.listview_roundcorner_item);
    lvListeFriends.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
            Friends item = (Friends) adapter.getItemAtPosition(position);
            if (item.getLongitude() != 0.0 && item.getLatitude() != 0.0) {
                destinationPoint = new GeoPoint(item.getLongitude(), item.getLatitude());
                markerDestination = putMarkerItem(markerDestination, destinationPoint, DEST_INDEX,
                        R.string.destination, R.drawable.marker_destination, -1);
                getRoadAsync();
                map.getController().setCenter(destinationPoint);
            } else {
                Toast.makeText(MainActivity.this, R.string.toast_friend_statut, Toast.LENGTH_LONG).show();
            }
        }
    });

    viewMapFilters = (ScrollView) this.findViewById(R.id.scrollViewMapFilters);
    viewMapFilters.setVisibility(View.GONE);

    // Initialiser tout ce qui est donnes utilisateur propres  l'activit
    init();

    getTools().relocateUser(mapController, map, myLocationOverlay, location);
}

From source file:com.mobeelizer.mobile.android.MobeelizerRealConnectionManager.java

private boolean isConnecting(final ConnectivityManager connectivityManager) {
    if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) == null) {
        return connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
    }// ww  w  . j av  a  2 s.c o  m
    return connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting()
            || connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
}

From source file:com.playhaven.android.req.PlayHavenRequest.java

protected static PlayHaven.ConnectionType getConnectionType(Context context) {
    try {/*from   ww  w  . j  a  v  a2s  .  c om*/
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (manager == null)
            return PlayHaven.ConnectionType.NO_NETWORK; // happens during tests

        NetworkInfo wifiInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wifiInfo != null) {
            NetworkInfo.State wifi = wifiInfo.getState();
            if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING)
                return PlayHaven.ConnectionType.WIFI;
        }

        NetworkInfo mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mobileInfo != null) {
            NetworkInfo.State mobile = mobileInfo.getState();
            if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING)
                return PlayHaven.ConnectionType.MOBILE;
        }
    } catch (SecurityException e) {
        // ACCESS_NETWORK_STATE permission not granted in the manifest
        return PlayHaven.ConnectionType.NO_PERMISSION;
    }

    return PlayHaven.ConnectionType.NO_NETWORK;
}

From source file:com.error.hunter.ListenService.java

private StringBuffer getNetworkInfo() {
    StringBuffer info = new StringBuffer();
    StringBuffer backUpInfo = new StringBuffer();

    final ConnectivityManager connMgr = (ConnectivityManager) 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.isConnected()) {
        info.append("\nWiFi network state: CONNECTED");
    } else {//from   ww w . j  a v  a2 s.c o m
        info.append("\nWiFi network state: NOT CONNECTED");
    }
    backUpInfo = new StringBuffer(info);
    try {
        if (!mobile.isConnected()) {
            switch (mobile.getSubtype()) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
                info.append("\nMobile network state: CONNECTED (GPRS)");
                break;
            case TelephonyManager.NETWORK_TYPE_EDGE:
                info.append("\nMobile network state: CONNECTED (EDGE)");
                break;
            case TelephonyManager.NETWORK_TYPE_CDMA:
                info.append("\nMobile network state: CONNECTED (CDMA)");
                break;
            case TelephonyManager.NETWORK_TYPE_HSPA:
                info.append("\nMobile network state: CONNECTED (HSPA)");
                break;
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                info.append("\nMobile network state: CONNECTED (HSPA+)");
                break;
            case TelephonyManager.NETWORK_TYPE_LTE:
                info.append("\nMobile network state: CONNECTED (LTE)");
                break;
            case TelephonyManager.NETWORK_TYPE_UMTS:
                info.append("\nMobile network state: CONNECTED (UMTS)");
                break;
            }
        } else {
            info.append("\nMobile network state: NOT CONNECTED");
        }
    } catch (Exception e) {
        e.printStackTrace();
        return backUpInfo;
    }
    return info;
}

From source file:com.mobeelizer.mobile.android.MobeelizerRealConnectionManager.java

private boolean isConnected(final ConnectivityManager connectivityManager) {
    if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) == null) {
        return connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
    }//from   ww w.j  av  a 2 s  . c o m
    return connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()
            || connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
}

From source file:com.odo.kcl.mobileminer.miner.MinerService.java

private void connectivityChanged() {
    String name = "None";
    ConnectivityManager manager = (ConnectivityManager) getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = manager.getActiveNetworkInfo();
    if (netInfo != null) {
        if (netInfo.getState() == NetworkInfo.State.CONNECTED) {
            switch (netInfo.getType()) {
            case ConnectivityManager.TYPE_WIFI:
                wifiData = true;/*from ww  w.  j a v a  2s.c om*/
                mobileData = false;
                if (!updating)
                    startUpdating();
                WifiManager wifiMgr = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
                name = wifiInfo.getSSID();
                if (!networkName.equals(name)) {
                    wirelessData = new WifiData(wifiInfo);
                    MinerData helper = new MinerData(context);
                    helper.putWifiNetwork(helper.getWritableDatabase(), wirelessData, new Date());
                    helper.close();
                    networkName = name;
                    networkBroadcast();
                }
                startScan(); // Always scan when we've got WIFI.
                //Log.i("MinerService","CONNECTED: WIFI");
                break;
            case ConnectivityManager.TYPE_MOBILE:
                wifiData = false;
                mobileData = true;
                if ("goldfish".equals(Build.HARDWARE)) {
                    if (!updating)
                        startUpdating();
                } else {
                    updating = false;
                }

                // https://code.google.com/p/android/issues/detail?id=24227
                //String name; Cursor c;
                //c = this.getContentResolver().query(Uri.parse("content://telephony/carriers/preferapn"), null, null, null, null);
                //name = c.getString(c.getColumnIndex("name"));
                TelephonyManager telephonyManager = ((TelephonyManager) this
                        .getSystemService(Context.TELEPHONY_SERVICE));
                name = telephonyManager.getNetworkOperatorName();
                if (!networkName.equals(name)) {
                    MinerData helper = new MinerData(context);
                    helper.putMobileNetwork(helper.getWritableDatabase(), telephonyManager, new Date());
                    helper.close();
                    networkName = name;
                    networkBroadcast();
                }
                //startScan();
                //Log.i("MinerService","CONNECTED MOBILE: "+name);
                break;
            default:
                //Log.i("MinerService",netInfo.getTypeName());
                break;
            }
        } else {
            scanning = false;
        }
    } else {
        scanning = false;
        networkName = "null";
    }

}

From source file:com.dmsl.anyplace.utils.NetworkUtils.java

public static boolean isOnlineWiFiOrMobile(Context context) {
    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    boolean isWifiConn = networkInfo.isConnected();
    networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    boolean isMobileConn = networkInfo.isConnected();
    return isMobileConn || isWifiConn;
}

From source file:com.android.providers.downloads.DownloadService.java

/**
 * Initializes the service when it is first created
 *///from   w  ww.jav a 2 s. com
@Override
public void onCreate() {
    super.onCreate();

    XLConfig.LOGD("(onCreate) ---> Service onCreate");

    if (mSystemFacade == null) {
        mSystemFacade = new RealSystemFacade(this);
    }

    mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    mStorageManager = new StorageManager(this);

    mUpdateThread = new HandlerThread(Constants.TAG + "-UpdateThread");
    mUpdateThread.start();
    mUpdateHandler = new Handler(mUpdateThread.getLooper(), mUpdateCallback);

    mScanner = new DownloadScanner(this);

    mNotifier = new DownloadNotifier(this);
    mNotifier.cancelAll();

    mObserver = new DownloadManagerContentObserver();
    getContentResolver().registerContentObserver(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, true, mObserver);

    // get mXunleiEngineEnable from DB
    mXunleiEngineEnable = Helpers.getXunleiUsagePermission(getApplicationContext());
    if (mXunleiEngineEnable) {
        startGetXlTokenEx(false);
        initXunleiEngine();
    }

    if (!miui.os.Build.IS_TABLET) {
        mCdnThread = new CdnQueryingThread();
        mCdnThread.start();
    }

    if (XLUtil.getNetwrokType(getApplicationContext()) == ConnectivityManager.TYPE_MOBILE) {
        mCloudControlThread = new MobileCloudCheckThread();
        mCloudControlThread.start();
    }

    String pkgName = getApplicationContext().getPackageName();
    Helpers.trackDownloadServiceStatus(this.getApplicationContext(), DOWNLOAD_SERVICE_START, pkgName);
    // do track
    //        Context ctx = getApplicationContext();
    //        String pkgName = ctx.getPackageName();
    //        Helpers.trackOnlineStatus(ctx, 0, 0, mXunleiEngineEnable, "", "", pkgName, PRODUCT_NAME, PRODUCT_VERSION);
}

From source file:mobile.tiis.appv2.LoginActivity.java

/**
 * function to check if there is an internet connectivity
 *//*from w  ww  .  j  a va  2  s  .  c  om*/
public boolean isInternetAvailable() {
    ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null && wifiNetwork.isConnected()) {
        return true;
    }
    NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mobileNetwork != null && mobileNetwork.isConnected()) {
        return true;
    }
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected()) {
        return true;
    }
    return false;
}