Example usage for android.net NetworkInfo isConnected

List of usage examples for android.net NetworkInfo isConnected

Introduction

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

Prototype

@Deprecated
public boolean isConnected() 

Source Link

Document

Indicates whether network connectivity exists and it is possible to establish connections and pass data.

Usage

From source file:com.undatech.opaque.RemoteCanvas.java

/**
 * Checks whether the device has networking and quits with an error if it doesn't.
 *//* ww w  . j  a  v  a 2s .  com*/
private void checkNetworkConnectivity() {
    ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork == null || !activeNetwork.isAvailable() || !activeNetwork.isConnected()) {
        disconnectAndShowMessage(R.string.error_not_connected_to_network, R.string.error_dialog_title);
    }
}

From source file:edu.mit.mobile.android.locast.data.Sync.java

private boolean isDataConnected() {
    // a check is done here as well as startSync() to avoid the queue getting filled when the network is down.
    final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo ni = cm.getActiveNetworkInfo();
    if (ni == null || !ni.isConnected()) {
        if (mNotifiedUserAboutNetworkStatus) {
            Toast.makeText(this, R.string.error_sync_no_data_network, Toast.LENGTH_LONG).show();
            mNotifiedUserAboutNetworkStatus = false;
        }/*from w w  w .  ja v  a 2  s. c o  m*/
        if (DEBUG) {
            Log.d(TAG, "not synchronizing, as it appears that there's no network connection");
        }
        return false;
    } else {
        return ni.isConnected();
    }

}

From source file:com.marianhello.cordova.bgloc.LocationUpdateService.java

private boolean isNetworkConnected() {
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null) {
            Log.d(TAG, "Network found, type = " + networkInfo.getTypeName());
            return networkInfo.isConnected();
        } else {//from www .j  ava  2s  .c o  m
            Log.d(TAG, "No active network info");
            return false;
        }
    }

From source file:com.connectsdk.discovery.DiscoveryManager.java

/**
 * Start scanning for devices on the local network.
 *//*from  w ww.  j  a va  2s. c o  m*/
public void start() {
    if (mSearching)
        return;

    if (discoveryProviders == null) {
        return;
    }

    mSearching = true;
    multicastLock.acquire();

    Util.runOnUI(new Runnable() {

        @Override
        public void run() {
            if (discoveryProviders.size() == 0) {
                registerDefaultDeviceTypes();
            }

            ConnectivityManager connManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (mWifi.isConnected()) {
                for (DiscoveryProvider provider : discoveryProviders) {
                    provider.start();
                }
            } else {
                Log.w("Connect SDK", "Wifi is not connected yet");

                Util.runOnUI(new Runnable() {

                    @Override
                    public void run() {
                        for (DiscoveryManagerListener listener : discoveryListeners)
                            listener.onDiscoveryFailed(DiscoveryManager.this,
                                    new ServiceCommandError(0, "No wifi connection", null));
                    }
                });
            }
        }
    });
}

From source file:com.example.mapdemo.BasicMapDemoActivity.java

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    if (netInfo == null) {
        System.out.println("Internet Connection Not Present");
        return false;
    }/*from  w ww.java  2s  .  c  om*/
    //Log.d("Network Status: ", netInfo.getState().toString());
    if (netInfo != null && netInfo.isConnected()) {
        if (netInfo.isAvailable()) {
            return true;
        } else {
            System.out.println("Internet Connection Not Present");
            return false;
        }

    } else {
        System.out.println("Internet Connection Not Present");
        return false;
    }
}

From source file:at.alladin.rmbt.android.main.RMBTMainActivity.java

/**
 * //from  w w  w  .  jav a 2 s. c  om
 * @param context
 * @return
 */
public static boolean isNetworkAvailable(Context context) {
    boolean isMobile = false, isWifi = false;

    try {
        NetworkInfo[] infoAvailableNetworks = ((ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE)).getAllNetworkInfo();

        if (infoAvailableNetworks != null) {
            for (NetworkInfo network : infoAvailableNetworks) {

                if (network.getType() == ConnectivityManager.TYPE_WIFI) {
                    if (network.isConnected() && network.isAvailable())
                        isWifi = true;
                }
                if (network.getType() == ConnectivityManager.TYPE_MOBILE) {
                    if (network.isConnected() && network.isAvailable())
                        isMobile = true;
                }
            }
        }

        return isMobile || isWifi;
    } catch (Exception e) {
        return false;
    }
}

From source file:com.wojtechnology.sunami.TheBrain.java

public boolean isSoundcloudEnabled() {
    if (!mMainPrefs.isSoundcloudEnabled)
        return false;
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(
            Service.CONNECTIVITY_SERVICE);
    NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo connection = connectivityManager.getActiveNetworkInfo();
    return wifi.isConnected() || (!mMainPrefs.forceOverWifi && connection != null && connection.isConnected());
}

From source file:carsharing.starter.automotive.iot.ibm.com.mobilestarterapp.AnalyzeMyDriving.java

private void getAccurateLocation(final GoogleMap googleMap) {
    final FragmentActivity activity = getActivity();
    if (activity == null) {
        Log.e("getAccurateLocation", "do nothing as getActivity()==null");
        if (locationManager != null) {
            requestLocationUpdates(false);
        }//from   ww w  .  j a va 2s .c o  m
        return;
    }
    final ConnectivityManager connectivityManager = (ConnectivityManager) activity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    final boolean networkConnected = networkInfo != null && networkInfo.isConnected();
    if (gpsEnabled && networkConnected) {
        if (!checkCurrentLocation()) {
            Log.e("Location Data", "Not Working!");
            //                Toast.makeText(getActivity().getApplicationContext(), "Please activate your location settings and restart the application!", Toast.LENGTH_LONG).show();
            getAccurateLocation(mMap);
            return;
        }
        if (mMap != null) {
            float zoom = mMap.getCameraPosition().zoom;
            zoom = Math.max(14, zoom);
            zoom = Math.min(18, zoom);

            mMap.clear();
            final LatLng newLocation = new LatLng(location.getLatitude(), location.getLongitude());
            //                if (!cameraSet) {
            mMap.moveCamera(CameraUpdateFactory.newLatLng(newLocation));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(zoom), 2000, null);
            //                    cameraSet = true;
            //                }
            mMap.addMarker(new MarkerOptions().position(newLocation).title("Your Location")
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.models)));
        }
        final ActionBar supportActionBar = ((AppCompatActivity) activity).getSupportActionBar();
        if (startedDriving) {
            if (!behaviorDemo) {
                // get credentials may be failed
                startedDriving = false;
                completeReservation(reservationId[0], false);
                return;
            }
            speedMessage = "" + Math.round(location.getSpeed() * 60 * 60 / 16.0934) / 100.0 + " MPH";
            supportActionBar.setTitle(speedMessage + " - Data not sent");

            tripCount += 1;
            if (tripCount % 10 == 0) {
                //                renderMapMatchedLocation()
            }
        } else if (deviceClient != null) {
            supportActionBar.setTitle("Press Start Driving when ready.");
        }
        if (behaviorDemo) {
            API.runInAsyncUIThread(new Runnable() {
                @Override
                public void run() {
                    if (deviceClient == null && needCredentials) {
                        createDeviceClient();
                        connectDeviceClient();
                    }
                    if (userUnlocked) {
                        sendLocation(location);
                    }
                }
            }, activity);
        }
    } else {
        if (!gpsEnabled) {
            Toast.makeText(activity.getApplicationContext(), "Please turn on your GPS", Toast.LENGTH_LONG)
                    .show();

            final Intent gpsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivityForResult(gpsIntent, GPS_INTENT);

            if (!networkConnected) {
                networkIntentNeeded = true;
            }
        } else if (!networkConnected) {
            Toast.makeText(activity.getApplicationContext(), "Please turn on Mobile Data or WIFI",
                    Toast.LENGTH_LONG).show();

            final Intent settingsIntent = new Intent(Settings.ACTION_SETTINGS);
            startActivityForResult(settingsIntent, SETTINGS_INTENT);
        }
    }
}

From source file:com.xmobileapp.rockplayer.LastFmEventImporter.java

/*********************************
 * //from   w  w w.ja v a  2 s . c o  m
 * Constructor
 * @param context
 * 
 *********************************/
public LastFmEventImporter(Context context) {
    this.context = context;

    Log.i("LASTFMEVENT", "creating-------------------------");
    /*
     * Check for Internet Connection (Through whichever interface)
     */
    ConnectivityManager connManager = (ConnectivityManager) ((RockPlayer) context)
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = connManager.getActiveNetworkInfo();
    /******* EMULATOR HACK - false condition needs to be removed *****/
    //if (false && (netInfo == null || !netInfo.isConnected())){
    if ((netInfo == null || !netInfo.isConnected())) {
        Bundle data = new Bundle();
        data.putString("info", "No Internet Connection");
        Message msg = new Message();
        msg.setData(data);
        ((RockPlayer) context).analyseConcertInfoHandler.sendMessage(msg);
        return;
    }

    /*
     * Get location
     */
    MIN_UPDATE_INTVL = 5 * 24 * 60 * 60 * 1000; // 5 days
    SPREAD_INTVL = 21 * 24 * 60 * 60 * 1000; // 21 days;
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    LocationManager locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    if (locManager.getBestProvider(criteria, true) != null)
        myLocation = locManager.getLastKnownLocation(locManager.getBestProvider(criteria, true));
    else {
        myLocation = new Location("gps");
        myLocation.setLatitude(47.100301);
        myLocation.setLongitude(-119.982465);
    }

    /*
     * Get preferred distance
     */
    //      SharedPreferences prefs = ((Filex) context).getSharedPreferences(((Filex) context).PREFS_NAME, 0);
    RockOnPreferenceManager prefs = new RockOnPreferenceManager(((RockPlayer) context).FILEX_PREFERENCES_PATH);
    concertRadius = prefs.getLong("ConcertRadius", (long) (((RockPlayer) context).CONCERT_RADIUS_DEFAULT));

    //myLocation =  locManager.getLastKnownLocation(locManager.getBestProvider(Criteria.POWER_LOW, true));

    //      try {
    //         getArtistEvents();
    //      } catch (SAXException e) {
    //         e.printStackTrace();
    //      } catch (ParserConfigurationException e) {
    //         e.printStackTrace();
    //      }
}

From source file:com.acc.android.network.operator.base.BaseHttpOperator.java

public static boolean hasInternet(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = manager.getActiveNetworkInfo();
    if (info == null || !info.isConnected()) {
        return false;
    }// ww  w. jav  a 2  s. com
    if (info.isRoaming()) {
        return true;
    }
    return true;
}