Example usage for android.net NetworkInfo isConnectedOrConnecting

List of usage examples for android.net NetworkInfo isConnectedOrConnecting

Introduction

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

Prototype

@Deprecated
public boolean isConnectedOrConnecting() 

Source Link

Document

Indicates whether network connectivity exists or is in the process of being established.

Usage

From source file:ca.zadrox.dota2esportticker.service.UpdateMatchService.java

private boolean checkForConnectivity() {
    ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

    return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}

From source file:it.unipr.informatica.autobusparma.MappaFragment.java

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }/*from  w w w. j a v a2s. c  o  m*/
    return false;
}

From source file:com.shearosario.tothepathandback.ClosestStationsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_closest_stations);

    getActionBar().setDisplayHomeAsUpEnabled(true);

    Intent intent = getIntent();/*w ww  .  ja va  2s.c o m*/
    context = this;
    activity = this;

    adView = (AdView) this.findViewById(R.id.adViewClosest);
    AdRequest adRequest = new AdRequest.Builder().addTestDevice("949F5429A9EC251C1DD4395558D33531").build();
    // AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

    if (intent.hasExtra("Manual")) {
        double[] manual = intent.getDoubleArrayExtra("Manual");
        origin = new LatLng(manual[0], manual[1]);
    } else if (intent.hasExtra("Current")) {
        double[] current = intent.getDoubleArrayExtra("Current");
        origin = new LatLng(current[0], current[1]);
    }

    double[] entranceMeasures = intent.getDoubleArrayExtra("closestSortMeasures");
    ArrayList<Entrance> closestEntrances = intent.getParcelableArrayListExtra("closestEntrances");

    MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, R.layout.listitem, closestEntrances,
            entranceMeasures);
    //ListAdapter adapter = createListAdapter(allStationsSortDistance);
    ListView listview = (ListView) findViewById(R.id.ClosestStationsList);
    listview.setAdapter(adapter);
    //setListAdapter(adapter);

    final GoogleMap gMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapview)).getMap();
    gMap.setMyLocationEnabled(false);
    gMap.addMarker(new MarkerOptions().title("Origin").position(origin));
    gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(origin, 13));
    gMap.setBuildingsEnabled(false);
    gMap.getUiSettings().setZoomControlsEnabled(false);

    TextView textView = (TextView) findViewById(R.id.osm_directions);
    textView.setText(Html.fromHtml("Data provided by  OpenStreetMap contributors "
            + "<a href=\"http://www.openstreetmap.org/copyright\">License</a>"));
    textView.setMovementMethod(LinkMovementMethod.getInstance());

    textView = (TextView) findViewById(R.id.directions_text);
    textView.setText(Html.fromHtml(
            "Directions, Nominatim Search Courtesy of " + "<a href=\"http://www.mapquest.com\">MapQuest</a>"));
    textView.setMovementMethod(LinkMovementMethod.getInstance());

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final Entrance item = (Entrance) parent.getItemAtPosition(position);

            gMap.clear();

            LatLngBounds bounds = new LatLngBounds.Builder().include(origin)
                    .include(new LatLng(item.getEntranceLocation()[0], item.getEntranceLocation()[1])).build();

            gMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 150));

            gMap.addMarker(new MarkerOptions().title("Origin").position(origin));

            String stationName = null;
            for (int i = 0; i < MainActivity.getAllStations().size(); i++) {
                if (item.getStopid().equalsIgnoreCase(MainActivity.getAllStations().get(i).getStopID())) {
                    stationName = MainActivity.getAllStations().get(i).getStopName();
                    break;
                }
            }

            gMap.addMarker(new MarkerOptions()
                    // .title(item.getStationName())
                    .title(stationName)
                    .position(new LatLng(item.getEntranceLocation()[0], item.getEntranceLocation()[1])));

            Button button = (Button) findViewById(R.id.button_destination);
            button.setEnabled(true);
            // button.setText("Select " + item.getStationName());
            button.setText("Select " + stationName);

            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    /*
                     * To check if the phone is currently using a network connection. 
                     * Listens to broadcasts when the the device is or is not connected to 
                     * a network
                     */
                    ConnectivityManager cm = (ConnectivityManager) context
                            .getSystemService(Context.CONNECTIVITY_SERVICE);
                    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
                    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
                    if (!isConnected) {
                        Toast.makeText(context, "No network connection", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    new DisplayDirectionsIntent(context, activity, origin, item);
                }
            });
        }
    });
}

From source file:com.amazonaws.mobileconnectors.pinpoint.internal.core.system.AndroidConnectivity.java

private void determineAvailability() {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    inAirplaneMode = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
            0) != 0;/*w  w w .  j a va 2  s.c o  m*/
    log.info("Airplane mode: " + inAirplaneMode);
    final NetworkInfo networkInfo = cm != null ? cm.getActiveNetworkInfo() : null;
    int networkType = 0;
    // default state
    hasWifi = false;
    // when we have connectivity manager, we assume we have some sort of
    // connectivity
    hasMobile = cm != null;
    // can we obtain network info?
    if (networkInfo != null) {
        if (networkInfo.isConnectedOrConnecting()) {
            networkType = networkInfo.getType();

            hasWifi = networkType == ConnectivityManager.TYPE_WIFI
                    || networkType == ConnectivityManager.TYPE_WIMAX;
            hasMobile = networkType == ConnectivityManager.TYPE_MOBILE
                    || networkType == ConnectivityManager.TYPE_MOBILE_DUN
                    || networkType == ConnectivityManager.TYPE_MOBILE_HIPRI
                    || networkType == ConnectivityManager.TYPE_MOBILE_MMS
                    || networkType == ConnectivityManager.TYPE_MOBILE_SUPL;
        } else {
            // if neither connected or connecting then hasMobile defaults
            // need to be changed to false
            hasMobile = false;
        }
    }
    log.info(String.format("Device Connectivity (%s)",
            hasWifi ? "On Wifi" : (hasMobile ? "On Mobile" : "No network connectivity")));
}

From source file:com.adnanbal.fxdedektifi.forex.presentation.view.fragment.AccountAndSubscriptionsFragment.java

/**
 * Checks if the device has any active internet connection.
 *
 * @return true device with internet connection, otherwise false.
 *//*from   w w  w  .  jav  a  2 s.co  m*/
private boolean isThereInternetConnection() {
    boolean isConnected;

    ConnectivityManager connectivityManager = (ConnectivityManager) getContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    isConnected = (networkInfo != null && networkInfo.isConnectedOrConnecting());

    return isConnected;
}

From source file:ch.ethz.coss.nervousnet.vm.sensors.ConnectivitySensor.java

public void runConnectivitySensor() {
    Log.d(LOG_TAG, "Inside runConnectivitySensor");
    if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(context,
            android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;//from  www  .  jav  a 2s.c  o m
    }
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    int networkType = -1;
    boolean isRoaming = false;
    if (isConnected) {
        networkType = activeNetwork.getType();
        isRoaming = activeNetwork.isRoaming();
    }

    String wifiHashId = "";
    int wifiStrength = Integer.MIN_VALUE;

    if (networkType == ConnectivityManager.TYPE_WIFI) {
        WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wi = wm.getConnectionInfo();
        StringBuilder wifiInfoBuilder = new StringBuilder();
        wifiInfoBuilder.append(wi.getBSSID());
        wifiInfoBuilder.append(wi.getSSID());
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(wifiInfoBuilder.toString().getBytes());
            wifiHashId = new String(messageDigest.digest());
        } catch (NoSuchAlgorithmException e) {
        }
        wifiStrength = wi.getRssi();
    }

    byte[] cdmaHashId = new byte[32];
    byte[] lteHashId = new byte[32];
    byte[] gsmHashId = new byte[32];
    byte[] wcdmaHashId = new byte[32];

    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    List<CellInfo> cis = tm.getAllCellInfo();
    if (cis != null) {
        // New method
        for (CellInfo ci : cis) {
            if (ci.isRegistered()) {
                if (ci instanceof CellInfoCdma) {
                    CellInfoCdma cic = (CellInfoCdma) ci;
                    cdmaHashId = generateMobileDigestId(cic.getCellIdentity().getSystemId(),
                            cic.getCellIdentity().getNetworkId(), cic.getCellIdentity().getBasestationId());
                }
                if (ci instanceof CellInfoGsm) {
                    CellInfoGsm cic = (CellInfoGsm) ci;
                    gsmHashId = generateMobileDigestId(cic.getCellIdentity().getMcc(),
                            cic.getCellIdentity().getMnc(), cic.getCellIdentity().getCid());
                }
                if (ci instanceof CellInfoLte) {
                    CellInfoLte cic = (CellInfoLte) ci;
                    lteHashId = generateMobileDigestId(cic.getCellIdentity().getMcc(),
                            cic.getCellIdentity().getMnc(), cic.getCellIdentity().getCi());
                }
                if (ci instanceof CellInfoWcdma) {
                    CellInfoWcdma cic = (CellInfoWcdma) ci;
                    wcdmaHashId = generateMobileDigestId(cic.getCellIdentity().getMcc(),
                            cic.getCellIdentity().getMnc(), cic.getCellIdentity().getCid());
                }
            }
        }
    } else {
        // Legacy method
        CellLocation cl = tm.getCellLocation();
        if (cl instanceof CdmaCellLocation) {
            CdmaCellLocation cic = (CdmaCellLocation) cl;
            cdmaHashId = generateMobileDigestId(cic.getSystemId(), cic.getNetworkId(), cic.getBaseStationId());
        }
        if (cl instanceof GsmCellLocation) {
            GsmCellLocation cic = (GsmCellLocation) cl;
            gsmHashId = generateMobileDigestId(cic.getLac(), 0, cic.getCid());
        }
    }

    StringBuilder mobileHashBuilder = new StringBuilder();
    mobileHashBuilder.append(new String(cdmaHashId));
    mobileHashBuilder.append(new String(lteHashId));
    mobileHashBuilder.append(new String(gsmHashId));
    mobileHashBuilder.append(new String(wcdmaHashId));

    dataReady(new ConnectivityReading(System.currentTimeMillis(), isConnected, networkType, isRoaming,
            wifiHashId, wifiStrength, mobileHashBuilder.toString()));

}

From source file:com.itsherpa.andg.imageloader.ImageFetcher.java

/**
 * Simple network connection check./*from   w  w  w  . j ava 2s.  com*/
 * 
 * @param context
 */
private void checkConnection(Context context) {
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    if (networkInfo == null || !networkInfo.isConnectedOrConnecting()) {
        LogUtils.e(TAG, "checkConnection - no connection found");
    }
}

From source file:net.mEmoZz.PopMovies.frags.MoviesFragment.java

private boolean isNetworkAvailable() {
    ConnectivityManager mConnectivity = (ConnectivityManager) getActivity()
            .getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo mNetworkInfo = mConnectivity.getActiveNetworkInfo();
    return mNetworkInfo != null && mNetworkInfo.isConnectedOrConnecting();
}

From source file:syncthing.android.service.ServiceSettings.java

boolean hasSuitableConnection() {
    NetworkInfo info = cm.getActiveNetworkInfo();
    if (onlyOnWifi()) {
        if (info != null && info.isConnectedOrConnecting()) {
            if (!isWifiOrEthernet(info.getType())) {
                Timber.d("Connection is not wifi network");
                return false;
            } else if (isWifi(info.getType()) && !isConnectedToWhitelistedNetwork()) {
                Timber.d("Connected wifi network not in whitelist");
                return false;
            } else {
                Timber.d("Connected to wifi or ethernet");
                return true;
            }/*from w  w  w.  ja  v  a  2 s . c  om*/
        } else {
            Timber.d("No wifi or ethernet connection");
            return false;
        }
    } else if (info != null && info.isConnectedOrConnecting()) {
        Timber.d("Connected to network");
        return true;
    } else {
        Timber.d("Not connected to any networks... running anyway");
        //TODO add setting to only run when connected
        return true;
    }
}

From source file:com.ifeng.util.imagecache.ImageFetcher.java

/**
 * Simple network connection check./*w ww  .ja  v a  2  s  .c o m*/
 * 
 * @param context
 */
private void checkConnection(Context context) {
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    if (networkInfo == null || !networkInfo.isConnectedOrConnecting()) {
        // TODO ???
        // Toast.makeText(context, R.string.no_network_connection_toast,
        // Toast.LENGTH_LONG).show();
        Log.e(TAG, "checkConnection - no connection found");
    }
}