private void startLocationMoitoring() { if (googleApiClient.isConnected()) { LocationRequest locationRequest = LocationRequest.create() .setInterval(10000) .setFastestInterval(5000) .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, new LocationListener() { @Override public void onLocationChanged(Location location) { Log.d(TAG, "location update"); currentLocation = location; } }); } }
/** * Tests for requestLocation. * * @throws Exception */ @Test public void requestLocation() throws Exception { GoogleApiClient mockGoogleApiClient = mock(GoogleApiClient.class); doReturn(true).when(mockGoogleApiClient).isConnected(); LocationManager mockLocationManager = spy(mLocationManager); Whitebox.setInternalState(mockLocationManager, "googleApiClient", mockGoogleApiClient); FusedLocationProviderApi mockLocationProviderApi = mock(FusedLocationProviderApi.class); Whitebox.setInternalState(LocationServices.class, "FusedLocationApi", mockLocationProviderApi); // Testing when a customer did not disableLocationCollection. Whitebox.invokeMethod(mockLocationManager, "requestLocation"); verify(mockLocationProviderApi).requestLocationUpdates(any(GoogleApiClient.class), any(LocationRequest.class), any(LocationListener.class)); // Testing when a customer disableLocationCollection. Leanplum.disableLocationCollection(); Whitebox.invokeMethod(mockLocationManager, "requestLocation"); verifyNoMoreInteractions(mockLocationProviderApi); }
private void findImage() { LocationRequest request = LocationRequest.create(); request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); request.setNumUpdates(1); request.setInterval(0); try { LocationServices.FusedLocationApi.requestLocationUpdates(mClient, request, new LocationListener() { @Override public void onLocationChanged(Location location) { Log.d(TAG, "onLocationChanged: " + location.getLatitude() + ", " + location.getLongitude()); new SearchTask().execute(location); } }); } catch (SecurityException e) { if (DEBUG) { Log.e(TAG, "no permission", e); } } }
private void startLocationUpdate( GoogleApiClient googleApiClient, LocationListener locationListener ) { boolean a = ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED; boolean b = ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED; if (a && b) { googleApiClient.disconnect(); return; } LocationServices.FusedLocationApi.requestLocationUpdates( googleApiClient, new LocationRequest().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY), locationListener); }
@Override public void onConnected(Bundle bundle) { initializeLocationRequest(10, 1000); LocationServices.FusedLocationApi.requestLocationUpdates(mClient, mLocationRequest, new LocationListener() { @Override public void onLocationChanged(Location location) { currentLocation = location; mapUtil.moveMap(mMap, currentLocation, 15); } }); currentLocation = LocationServices.FusedLocationApi.getLastLocation(mClient); if (currentLocation != null) { mapUtil.moveMap(mMap, currentLocation, 15); } }
private void startLocationManagerAfterLogin() { mProgressFacebook.setVisibility(View.VISIBLE); mFacebookLoginButton.setVisibility(View.INVISIBLE); mTwitterLoginButton.setVisibility(View.INVISIBLE); mInstaLoginButton.setVisibility(View.INVISIBLE); mLocationManager.startLocationUpdates(new LocationListener() { @Override public void onLocationChanged(Location location) { if (location != null) { handleLocationUpdate(location); } } }); }
@Override public void onConnected(@Nullable Bundle bundle) { LocationRequest mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(2000); mLocationRequest.setFastestInterval(1000); mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .addLocationRequest(mLocationRequest); LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build()); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, new LocationListener() { @Override public void onLocationChanged(Location location) { } }); }
@SuppressWarnings("MissingPermission") @Override public void onConnected(@Nullable Bundle bundle) { if (PermissionUtils.isLocationPermissionGranted(this)) { LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, mLocationRequest, new LocationListener() { @Override public void onLocationChanged(Location location) { currLocation = new LatLng(location.getLatitude(), location.getLongitude()); // Creating a LatLng object for the current location } }); Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); if (lastLocation != null) { currLocation = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude()); } else Toast.makeText(StoreDetailActivity.this, "Cannot get current location!", Toast.LENGTH_SHORT).show(); } else { PermissionUtils.requestLocaiton(this); } }
public void startLocationListening(Context context, LocationListener locationListener) { this.context = context; this.locationListener = locationListener; if (mGoogleApiClient == null) { mGoogleApiClient = new GoogleApiClient.Builder(context) .addConnectionCallbacks(this) .addApi(LocationServices.API) .build(); } mGoogleApiClient.connect(); mLocationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(MIN_UPDATE_INTERVAL_IN_SECONDS * 1000); }
private void checkLocationAndInit() { if (sLastLocation == null) { sLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); } if (sLastLocation == null) { LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, createLocationRequest(), new LocationListener() { @Override public void onLocationChanged(Location location) { sLastLocation = location; if (mCallback != null) { mCallback.onLocationChanged(sLastLocation); } initAppAfterCheckingLocation(); LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); } }); } else { initAppAfterCheckingLocation(); } }
@SuppressLint("MissingPermission") @Override protected void locationSettingSuccess(final ResourceObserver<Location> emitter, GoogleApiClient client) { this.client = client; locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { emitter.onNext(location); if (locationRequest.getNumUpdates() == 1) { emitter.onComplete(); } } }; //noinspection MissingPermission LocationServices.FusedLocationApi.requestLocationUpdates(client, locationRequest, locationListener); }
@Override @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) public Observable<Location> requestLocationUpdates(LocationRequest request) { return Observable.create(emitter -> { GoogleApiClient client = mBuilder.build(); LocationListener listener = emitter::onNext; GoogleApiClients.connect(client, hint -> FusedLocationApi .requestLocationUpdates(client, request, listener, Loopers.nullOrMain()) .setResultCallback(status -> { if (!status.isSuccess()) { emitter.onComplete(); } }), result -> emitter.onComplete()); emitter.setCancellable(() -> { if (client.isConnected()) { FusedLocationApi.removeLocationUpdates(client, listener); } client.disconnect(); }); }); }
public void requestLocationUpdates(LocationRequest request, final LocationListener listener) throws RemoteException { if (nativeLocation != null) { nativeLocation.requestLocationUpdates(request, listener); } else { if (!listenerMap.containsKey(listener)) { listenerMap.put(listener, new ILocationListener.Stub() { @Override public void onLocationChanged(Location location) throws RemoteException { listener.onLocationChanged(location); } }); } getServiceInterface().requestLocationUpdatesWithPackage(request, listenerMap.get(listener), getContext().getPackageName()); } }
public void requestLocationUpdates(LocationRequest paramLocationRequest, LocationListener paramLocationListener, Looper paramLooper) { this.xP.bP(); if (paramLooper == null) eg.b(Looper.myLooper(), "Can't create handler inside thread that has not called Looper.prepare()"); synchronized (this.xS) { b localb1 = (b)this.xS.get(paramLocationListener); b localb2 = localb1; if (localb1 == null) localb2 = new b(paramLocationListener, paramLooper); this.xS.put(paramLocationListener, localb2); try { ((gl)this.xP.bQ()).a(paramLocationRequest, localb2, this.mContext.getPackageName()); } catch (RemoteException localRemoteException) { throw new IllegalStateException(localRemoteException); } return; } }
@Override protected void onGoogleApiClientReady(GoogleApiClient apiClient, final Observer<? super Location> observer) { listener = new LocationListener() { @Override public void onLocationChanged(Location location) { observer.onNext(location); } }; LocationServices.FusedLocationApi.requestLocationUpdates(apiClient, locationRequest, listener); }
@Override protected void onGoogleApiClientReady(GoogleApiClient apiClient, final FlowableEmitter<Location> emitter) { locationListener = new LocationListener() { @Override public void onLocationChanged(Location value) { emitter.onNext(value); } }; //noinspection MissingPermission setupLocationPendingResult( LocationServices.FusedLocationApi.requestLocationUpdates(apiClient, locationRequest, locationListener, looper), new StatusErrorResultCallBack(emitter) ); }
@Override public void onConnected(LocationListener locationListener) { LocationRequest locationRequest = new LocationRequest() .setInterval(Constants.ACTIVITY_MODE) .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); try { LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, locationListener); }catch(SecurityException e){ } }
public void addListener(LocationListener listener) { if (mListeners.isEmpty() && listener != null && mGoogleApiClient != null) { mGoogleApiClient.connect(); } mListeners.add(listener); }
public void removeListener(LocationListener listener) { mListeners.remove(listener); if (mListeners.isEmpty() && mGoogleApiClient != null && mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } }
private void findImage() { LocationRequest request = LocationRequest.create(); request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); request.setNumUpdates(1); request.setInterval(0); LocationServices.FusedLocationApi .requestLocationUpdates(mClient, request, new LocationListener() { @Override public void onLocationChanged(Location location) { Log.i(TAG, "Got a fix: " + location); new SearchTask().execute(location); } }); }
private void findCoords() { LocationRequest request = LocationRequest.create(); request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); request.setNumUpdates(1); request.setInterval(0); LocationServices.FusedLocationApi .requestLocationUpdates(mClient, request, new LocationListener() { @Override public void onLocationChanged(Location location) { double lat = location.getLatitude(); double lon = location.getLongitude(); mLocation.setLatitude(lat); mLocation.setLongitude(lon); mWasLocationFixed = true; //Sort the order in which places are shown //depending on how far they are from us (= by distance ascending) Collections.sort(mPlaces, new Comparator<Visitable>() { @Override public int compare(Visitable a, Visitable b) { Location aLoc = a.getLocation(); Location bLoc = b.getLocation(); return (int) aLoc.distanceTo(mLocation) - (int) bLoc.distanceTo(mLocation); } }); mAdapter.setPlaces(mPlaces); mAdapter.notifyDataSetChanged(); } }); }
@Override protected void onHandleIntent(Intent intent) { if (intent != null) { final String address = intent.getStringExtra(BleService.DEVICE_ADDRESS); if (address != null) { final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(getApplicationContext()) .addApi(LocationServices.API) .build(); final LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(final Location location) { if (location != null) { updateDeviceLocation(address, location); LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this); googleApiClient.disconnect(); } } }; googleApiClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { @Override public void onConnected(@Nullable Bundle bundle) { startLocationUpdate(googleApiClient, locationListener); } @Override public void onConnectionSuspended(int i) { } }); googleApiClient.connect(); } } }
@Override public void onConnected(Bundle bundle) { for (LocationListener listener : mPendingListener.keySet()) { startLocationUpdates(listener, mPendingListener.get(listener)); } mPendingListener.clear(); if (getIntent() != null && getIntent().hasExtra(EXTRA_GEOFENCE_PLACE_ID)) { LocationServices.GeofencingApi.removeGeofences( mGoogleApiClient, Collections.singletonList(getIntent().getStringExtra(EXTRA_GEOFENCE_PLACE_ID)) ); getIntent().removeExtra(EXTRA_GEOFENCE_PLACE_ID); } }
void startLocationUpdates(LocationListener listener, LocationRequest locationRequest) { if (mGoogleApiClient.isConnected()) { LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, listener); } else { mPendingListener.put(listener, locationRequest); } }
void stopLocationUpdates(LocationListener listener) { if (mGoogleApiClient.isConnected()) { LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, listener); } else { mPendingListener.remove(listener); } }
/** * Overriding Callback {@link GoogleApiClient.ConnectionCallbacks} onConnected and finds the * current location of the user using {@link LocationRequest} and location services * * @param bundle * @see com.google.android.gms.location.FusedLocationProviderApi * @see GoogleApiClient */ @Override public void onConnected(Bundle bundle) { initializeLocationRequest(1, 1000); LocationServices.FusedLocationApi.requestLocationUpdates(mClient, mLocationRequest, new LocationListener() { @Override public void onLocationChanged(Location location) { currentLocation = location; } }); currentLocation = LocationServices.FusedLocationApi.getLastLocation(mClient); }
/** * Find the device's current location using location services * * @see LocationRequest * @see LocationServices * @see com.google.android.gms.location.FusedLocationProviderApi */ public void findCurrentLocation() { currentLocation = LocationServices.FusedLocationApi.getLastLocation(mClient); Log.i("ActivityDryverMain: ", "CURRENT LOCATION: " + currentLocation); //CODE BELOW IS FOR CONTINUOUSLY UPDATING USER LOCATION LocationServices.FusedLocationApi.requestLocationUpdates(mClient, mLocationRequest, new LocationListener() { @Override public void onLocationChanged(Location location) { currentLocation = location; Log.i("ActivityDryverMain: ", "NEW LOCATION: " + location); } }); }
@SuppressLint("HandlerLeak") private void getCurrentLocation() { if (isLocationServiceEnabled) { handleLocationRequestTimeout(); int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context); Log.d(SmartConstants.APP_NAME, "LocationService->play services:" + resp); if (resp == ConnectionResult.SUCCESS) { locationclient = new LocationClient(context, this, this); locationclient.connect(); showProgressDialog(); final LocationListener locListener = this; Handler locationHandler = new Handler() { @Override public void handleMessage(Message msg) { locationrequest = LocationRequest.create(); locationrequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationrequest.setInterval(TIME_INTERVAL_BW_REQUESTS); locationrequest.setNumUpdates(1); locationclient.requestLocationUpdates(locationrequest, locListener); } }; locationHandler.sendEmptyMessageDelayed(0, 1000); } else { onErrorLocationOperation(ExceptionTypes.LOCATION_ERROR_PLAY_SERVICE_NOT_AVAILABLE, ExceptionTypes.LOCATION_ERROR_PLAY_SERVICE_NOT_AVAILABLE_MESSAGE); } } else { // Means that the location service of the device is not turned on // Prompt the user to turn on the Location service showDecisionDialog("Location Service Disabled", "Location service is disabled in your device. Enable it?", "Enable Location", "No"); } }
@Override public void call(AsyncEmitter<Location> locationAsyncEmitter) { LocationListener locationListener = locationAsyncEmitter::onNext; GoogleApiClient.OnConnectionFailedListener onConnectionFailedListener = connectionResult -> locationAsyncEmitter.onError(new LocationUnavailableException("Failed to connect to Google Play Services!")); GoogleApiClient.ConnectionCallbacks connectionCallbacks = new GoogleApiClient.ConnectionCallbacks() { @Override public void onConnected(@Nullable Bundle bundle) { try { LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, locationListener); } catch (SecurityException e) { locationAsyncEmitter.onError(new LocationUnavailableException("Location permission not available!")); } } @Override public void onConnectionSuspended(int i) { locationAsyncEmitter.onError(new LocationUnavailableException("Connection lost to Google Play Services")); } }; googleApiClient.registerConnectionCallbacks(connectionCallbacks); googleApiClient.registerConnectionFailedListener(onConnectionFailedListener); googleApiClient.connect(); locationAsyncEmitter.setCancellation(() -> { LocationAsyncEmitter.this.locationRequest = null; LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, locationListener); googleApiClient.unregisterConnectionCallbacks(connectionCallbacks); googleApiClient.unregisterConnectionFailedListener(onConnectionFailedListener); }); }
private void startLocationManagerAfterLogin() { mProgress.setVisibility(View.VISIBLE); // mLoginButton.setVisibility(View.INVISIBLE); mFacebookLoginButton.setVisibility(View.INVISIBLE); mLocationManager.startLocationUpdates(new LocationListener() { @Override public void onLocationChanged(Location location) { if (location != null) { handleLocationUpdate(location); } } }); }
private void startLocationManagerAfterLogin() { mLocationManager.startLocationUpdates(new LocationListener() { @Override public void onLocationChanged(Location location) { if (location != null) { handleLocationUpdate(location); } } }); }
@Override public void onLocationChanged(Location location) { lastLocation = location; LocationListener listener; for(int i = 0; i < listeners.size(); i++) { listener = listeners.get(i); if(listener != null) { listener.onLocationChanged(location); } } }
/** * Handles the intent. * * @param intent the intent */ private void handleIntent(Intent intent) { if (!Intent.ACTION_SEARCH.equals(intent.getAction())) { Log.e(TAG, "Invalid intent action: " + intent); finish(); return; } final String textQuery = intent.getStringExtra(SearchManager.QUERY); setTitle(textQuery); final MyTracksLocationManager myTracksLocationManager = new MyTracksLocationManager( this, Looper.myLooper(), true); LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(final Location location) { myTracksLocationManager.close(); new Thread() { @Override public void run() { SearchQuery query = new SearchQuery( textQuery, location, -1L, System.currentTimeMillis()); doSearch(query); } }.start(); } }; myTracksLocationManager.requestLastLocation(locationListener); }
/** * Request last location. * * @param locationListener location listener */ public void requestLastLocation(final LocationListener locationListener) { handler.post(new Runnable() { @Override public void run() { if (!isAllowed()) { requestLastLocation = null; locationListener.onLocationChanged(null); } else { requestLastLocation = locationListener; connectionCallbacks.onConnected(null); } } }); }
/** * Requests location updates. This is an ongoing request, thus the caller * needs to check the status of {@link #isAllowed}. * * @param minTime the minimal time * @param minDistance the minimal distance * @param locationListener the location listener */ public void requestLocationUpdates( final long minTime, final float minDistance, final LocationListener locationListener) { handler.post(new Runnable() { @Override public void run() { requestLocationUpdatesTime = minTime; requestLocationUpdatesDistance = minDistance; requestLocationUpdates = locationListener; connectionCallbacks.onConnected(null); } }); }
/** * Removes location updates. * * @param locationListener the location listener */ public void removeLocationUpdates(final LocationListener locationListener) { handler.post(new Runnable() { @Override public void run() { requestLocationUpdates = null; if (locationClient != null && locationClient.isConnected()) { locationClient.removeLocationUpdates(locationListener); } } }); }
/** * This method is called after an activity type is selected from the selectActivityType method */ protected void startLocationUpdates() { locationListener = new LocationListener() { public void onLocationChanged(Location location) { //Only update location is we are recording if (recording) { updateLocation(location); } } }; if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, locationListener); } Log.i("Development", "startLocationUpdates"); }
/** * Configure parameters for requests to the fused location provider. * * @param callback LocationListener. */ void createLocationRequest(LocationListener callback) { locationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setInterval(UPDATE_INTERVAL) .setFastestInterval(FASTEST_INTERVAL); LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, callback); }
/** * Start requesting location updates. * * @param listener LocationListener. */ void startLocationUpdates(LocationListener listener) { if (locationRequest != null) { LocationServices.FusedLocationApi.requestLocationUpdates( googleApiClient, locationRequest, listener); } }