Example usage for android.location LocationManager NETWORK_PROVIDER

List of usage examples for android.location LocationManager NETWORK_PROVIDER

Introduction

In this page you can find the example usage for android.location LocationManager NETWORK_PROVIDER.

Prototype

String NETWORK_PROVIDER

To view the source code for android.location LocationManager NETWORK_PROVIDER.

Click Source Link

Document

Name of the network location provider.

Usage

From source file:com.example.programming.proximityalerts.ProximityAlertService.java

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Location bestLocation = null;

        latitude = intent.getDoubleExtra(LATITUDE_INTENT_KEY, Double.MIN_VALUE);
        longitude = intent.getDoubleExtra(LONGITUDE_INTENT_KEY, Double.MIN_VALUE);
        radius = intent.getFloatExtra(RADIUS_INTENT_KEY, Float.MIN_VALUE);

        for (String provider : locationManager.getProviders(false)) {
            if (ActivityCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(this,
                            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return TODO;
            }//  ww w.  j ava 2  s.  c  o m
            Location location = locationManager.getLastKnownLocation(provider);

            if (bestLocation == null) {
                bestLocation = location;
            } else {
                if (location.getAccuracy() < bestLocation.getAccuracy()) {
                    bestLocation = location;
                }
            }
        }

        if (bestLocation != null) {
            if (getDistance(bestLocation) <= radius) {
                inProximity = true;
            } else {
                inProximity = false;
            }
        }

        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

        return START_STICKY;
    }

From source file:pw.dedominic.csc311_final_project.MainActivity.java

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

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    MAC_ADDR = mBluetoothAdapter.getAddress();

    // ready GPS unit
    mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    mLocationListener = new LocationListener() {
        @Override/*from   w ww  .j a v a  2  s  .c  om*/
        public void onLocationChanged(Location location) {
            PLAYER_LATITUDE = location.getLatitude();
            PLAYER_LONGITUDE = location.getLongitude();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
    if (mLocationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER)) {
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener);
        PROVIDER = LocationManager.NETWORK_PROVIDER;
    } else {
        PROVIDER = LocationManager.GPS_PROVIDER;
    }

    mHttpService.recreateGetUserNameTask();
    mHttpService.getUserName(MAC_ADDR);
}

From source file:org.borderstone.tagtags.widgets.BGPSWidget.java

private void startLocationListener() {
    locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);

    try {/* ww w  . jav a 2 s.  c om*/
        gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ignored) {
    }
    try {
        network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception ignored) {
    }

    if (!gps_enabled && !network_enabled) {
        Toast.makeText(getContext(), "Location services are not active!", Toast.LENGTH_LONG).show();
    } else {
        if (network_enabled)
            provider = LocationManager.NETWORK_PROVIDER;

        if (gps_enabled)
            provider = LocationManager.GPS_PROVIDER;

        if (checkPermission())
            locationManager.requestLocationUpdates(provider, 0, 0, this);

        tick();
        setGPSButtonEnabled(true);
    }
}

From source file:com.samknows.measurement.schedule.datacollection.LocationDataCollector.java

@Override
public void start(TestContext tc) {
    super.start(tc);
    locations = Collections.synchronizedList(new ArrayList<Location>());
    manager = (LocationManager) tc.getSystemService(Context.LOCATION_SERVICE);

    locationType = AppSettings.getInstance().getLocationServiceType();
    //if the provider in the settings is gps but the service is not enable fail over to network provider
    if (locationType == LocationType.gps && !manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        locationType = LocationType.network;
    }//from  w  w  w .ja  va 2s. co m

    if (locationType != LocationType.gps && locationType != LocationType.network) {
        throw new RuntimeException("unknown location type: " + locationType);
    }

    String provider = locationType == LocationType.gps ? LocationManager.GPS_PROVIDER
            : LocationManager.NETWORK_PROVIDER;

    if (getLastKnown) {
        lastKnown = manager.getLastKnownLocation(provider);
        if (lastKnown != null) {
            data.add(new LocationData(true, lastKnown, locationType));
            lastKnownLocation = locationToDCSString("LASTKNOWNLOCATION", lastKnown);
        }
    }
    gotLastLocation = false;
    manager.requestLocationUpdates(provider, 0, 0, LocationDataCollector.this, Looper.getMainLooper());
    Logger.d(this, "start collecting location data from: " + provider);

    try {
        Logger.d(this, "sleeping: " + time);
        Thread.sleep(time);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    //stop listening for location updates if we are on network. That is done because network location uses network and breaks NetworkCondition
    if (locationType == LocationType.network) {
        manager.removeUpdates(this);
    }
}

From source file:com.nextgis.mobile.forms.CompassFragment.java

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

    // reference to vibrator service
    vibrator = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);

    // vibrate or not?
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    vibrationOn = prefs.getBoolean("compass_vibration", true);

    if (mCurrentLocation == null) {
        LocationManager locationManager = (LocationManager) getActivity()
                .getSystemService(Context.LOCATION_SERVICE);
        mCurrentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (mCurrentLocation == null) {
            mCurrentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }//from   ww  w .  ja  v  a 2s . com
    }
    mDeclination = 0;

    if (mCurrentLocation != null) {
        mDeclination = getDeclination(mCurrentLocation, System.currentTimeMillis());
    }

    sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
    sensorManager.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
            SensorManager.SENSOR_DELAY_NORMAL);

    getActivity().registerReceiver(compassBroadcastReceiver, new IntentFilter(ACTION_COMPASS_UPDATES));

    Log.d(TAG, "CompassActivity: onCreate");
}

From source file:com.example.android.uvdemo.MainActivity.java

private boolean initLocationProvider() {
    mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (mLocMgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        return true;
    }//from w  w  w .j a v a  2 s .  co  m
    return false;
}

From source file:polytech.carcassonnetour.MapsActivity.java

private void enableMyLocation() {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        PermissionUtils.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
                Manifest.permission.ACCESS_FINE_LOCATION, true);
    } else if (mMap != null) {
        mMap.setMyLocationEnabled(true);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
    }/*from w  w w  .  ja v a 2  s . c  o m*/
}

From source file:com.guidewithme.ArticleInfoListFragment.java

private void checkLocation() {
    if (System.currentTimeMillis() - sLastLocationRequestTime > LOCATION_UPDATE_INTERVAL) {
        // reqestSingleUpdate() listen for single update
        // and when get it will unsubscribe from LocationManager
        if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
            mLocationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null);
        if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
            mLocationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, this, null);
    }/*from   w ww  .j  a v  a2  s.com*/
}

From source file:com.kinvey.samples.citywatch.CityWatch.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Logger.getLogger(HttpTransport.class.getName()).setLevel(LOGGING_LEVEL);

    kinveyClient = ((CityWatchApplication) getApplication()).getClient();

    if (!kinveyClient.user().isUserLoggedIn()) {
        login();/* ww w  .ja v  a2s . co  m*/
    } else {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // Note there is no call to SetContentView(...) here.
        // Check out onTabSelected()-- the selected tab and associated
        // fragment
        // is
        // given
        // android.R.id.content as it's parent view, which *is* the content
        // view. Basically, this approach gives the fragments under the tabs
        // the
        // complete window available to our activity without any unnecessary
        // layout inflation.
        setUpTabs();

        curEntity = new CityWatchEntity();
        nearbyAddress = new ArrayList<Address>();
        nearbyEntities = new ArrayList<CityWatchEntity>();

        locationmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationmanager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 100, this);

        if (Geocoder.isPresent()) {
            geocoder = new Geocoder(this);
        }

        // get last known location for a quick, rough start. Try GPS, if
        // that
        // fails, try network. If that fails wait for fresh data
        lastKnown = locationmanager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (lastKnown == null) {
            lastKnown = locationmanager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }
        if (lastKnown == null) {
            // if the device has never known it's location, start at 0...?
            lastKnown = new Location(TAG);
            lastKnown.setLatitude(0.0);
            lastKnown.setLongitude(0.0);
        }
        Log.i(TAG, "lastKnown -> " + lastKnown.getLatitude() + ", " + lastKnown.getLongitude());
        setLocationInEntity(lastKnown);
    }

}

From source file:com.example.android.touroflondon.MainActivity.java

@Override
protected void onResume() {
    super.onResume();

    //Verify that Google Play Services is available
    int playStatus = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

    if (playStatus != ConnectionResult.SUCCESS) {
        // Google Play services is not available, prompt user and close application when dialog is dismissed
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(playStatus, this, 0);
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override/*from w  w w  .  j av  a2 s.  c  o m*/
            public void onCancel(DialogInterface dialog) {
                finish();
            }
        });
        dialog.show();

        // Hide all active fragments
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.hide(mMapFragment);
        if (mPoiListFragment != null) {
            ft.hide(mPoiListFragment);
        }
        ft.commit();

    } else {

        // Getting reference to the SupportMapFragment of activity_main.xml
        TourMapFragment fm = (TourMapFragment) getFragmentManager().findFragmentById(R.id.fragment_map);

        // Getting GoogleMap object from the fragment
        googleMap = fm.getMap();

        // Enabling MyLocation Layer of Google Map
        googleMap.setMyLocationEnabled(true);

        // Getting LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 20000, 0, mMapFragment);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 20000, 0, mMapFragment);
        //            Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        // Make sure active fragments are shown when returning from Play Services dialog interaction
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.show(mMapFragment);
        if (mPoiListFragment != null) {
            ft.show(mPoiListFragment);
        }
        ft.commit();

    }

}