com.google.android.gms.location.FusedLocationProviderApi Java Examples

The following examples show how to use com.google.android.gms.location.FusedLocationProviderApi. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UtilityService.java    From io2015-codelabs with Apache License 2.0 6 votes vote down vote up
/**
 * Called when the location has been updated
 */
private void locationUpdated(Intent intent) {
    Log.v(TAG, ACTION_LOCATION_UPDATED);

    // Extra new location
    Location location =
            intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);

    if (location != null) {
        LatLng latLngLocation = new LatLng(location.getLatitude(), location.getLongitude());

        // Store in a local preference as well
        Utils.storeLocation(this, latLngLocation);

        // Send a local broadcast so if an Activity is open it can respond
        // to the updated location
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }
}
 
Example #2
Source File: GeofenceServiceNormal.java    From arcgis-runtime-demos-android with Apache License 2.0 6 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
  if (intent != null) {
    final String action = intent.getAction();

    if (MainActivity.ACTION_CHECK_LOCATION.equals(action)) {
      final Location location = intent.getParcelableExtra(
          FusedLocationProviderApi.KEY_LOCATION_CHANGED);
      handleActionCheckLocation(location);
    }
    else if (MainActivity.ACTION_START_NORMAL_UPDATES.equals(action)) {
        handleActionStartNormalUpdates();
    }
    else if (MainActivity.ACTION_START_FAST_UPDATES.equals(action)) {
      handleActionChangeToFastUpdates();
    }
  }
}
 
Example #3
Source File: GeofenceServiceFast.java    From arcgis-runtime-demos-android with Apache License 2.0 6 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
  if (intent != null) {
    final String action = intent.getAction();

    if (MainActivity.ACTION_CHECK_LOCATION.equals(action)) {
      final Location location = intent.getParcelableExtra(
          FusedLocationProviderApi.KEY_LOCATION_CHANGED);
      handleActionCheckLocation(location);
    }
    else if (MainActivity.ACTION_START_NORMAL_UPDATES.equals(action)) {
      handleActionChangeToNormalUpdates();
    }
    else if (MainActivity.ACTION_START_FAST_UPDATES.equals(action)) {
      handleActionStartFastUpdates();
    }
  }
}
 
Example #4
Source File: GeofenceServiceNormal.java    From arcgis-runtime-demos-android with Apache License 2.0 6 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
  if (intent != null) {
    final String action = intent.getAction();

    if (MainActivity.ACTION_CHECK_LOCATION.equals(action)) {
      final Location location = intent.getParcelableExtra(
          FusedLocationProviderApi.KEY_LOCATION_CHANGED);
      handleActionCheckLocation(location);
    }
    else if (MainActivity.ACTION_START_NORMAL_UPDATES.equals(action)) {
        handleActionStartNormalUpdates();
    }
    else if (MainActivity.ACTION_START_FAST_UPDATES.equals(action)) {
      handleActionChangeToFastUpdates();
    }
  }
}
 
Example #5
Source File: GeofenceServiceFast.java    From arcgis-runtime-demos-android with Apache License 2.0 6 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
  if (intent != null) {
    final String action = intent.getAction();

    if (MainActivity.ACTION_CHECK_LOCATION.equals(action)) {
      final Location location = intent.getParcelableExtra(
          FusedLocationProviderApi.KEY_LOCATION_CHANGED);
      handleActionCheckLocation(location);
    }
    else if (MainActivity.ACTION_START_NORMAL_UPDATES.equals(action)) {
      handleActionChangeToNormalUpdates();
    }
    else if (MainActivity.ACTION_START_FAST_UPDATES.equals(action)) {
      handleActionStartFastUpdates();
    }
  }
}
 
Example #6
Source File: UtilityService.java    From io2015-codelabs with Apache License 2.0 6 votes vote down vote up
/**
 * Called when the location has been updated
 */
private void locationUpdated(Intent intent) {
    Log.v(TAG, ACTION_LOCATION_UPDATED);

    // Extra new location
    Location location =
            intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);

    if (location != null) {
        LatLng latLngLocation = new LatLng(location.getLatitude(), location.getLongitude());

        // Store in a local preference as well
        Utils.storeLocation(this, latLngLocation);

        // Send a local broadcast so if an Activity is open it can respond
        // to the updated location
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }
}
 
Example #7
Source File: LeanplumLocationManagerTest.java    From Leanplum-Android-SDK with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #8
Source File: UtilityService.java    From wear-os-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Called when the location has been updated
 */
private void locationUpdated(Intent intent) {
    Log.v(TAG, ACTION_LOCATION_UPDATED);

    // Extra new location
    Location location =
            intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);

    if (location != null) {
        LatLng latLngLocation = new LatLng(location.getLatitude(), location.getLongitude());

        // Store in a local preference as well
        Utils.storeLocation(this, latLngLocation);

        // Send a local broadcast so if an Activity is open it can respond
        // to the updated location
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }
}
 
Example #9
Source File: UtilityService.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
/**
 * Called when a location update is requested
 */
private void requestLocationInternal() {
    Log.v(TAG, ACTION_REQUEST_LOCATION);
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .build();

    // It's OK to use blockingConnect() here as we are running in an
    // IntentService that executes work on a separate (background) thread.
    ConnectionResult connectionResult = googleApiClient.blockingConnect(
            Constants.GOOGLE_API_CLIENT_TIMEOUT_S, TimeUnit.SECONDS);

    if (connectionResult.isSuccess() && googleApiClient.isConnected()) {

        Intent locationUpdatedIntent = new Intent(this, UtilityService.class);
        locationUpdatedIntent.setAction(ACTION_LOCATION_UPDATED);

        // Send last known location out first if available
        Location location = FusedLocationApi.getLastLocation(googleApiClient);
        if (location != null) {
            Intent lastLocationIntent = new Intent(locationUpdatedIntent);
            lastLocationIntent.putExtra(
                    FusedLocationProviderApi.KEY_LOCATION_CHANGED, location);
            startService(lastLocationIntent);
        }

        // Request new location
        LocationRequest mLocationRequest = new LocationRequest()
                .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        FusedLocationApi.requestLocationUpdates(
                googleApiClient, mLocationRequest,
                PendingIntent.getService(this, 0, locationUpdatedIntent, 0));

        googleApiClient.disconnect();
    } else {
        Log.e(TAG, String.format(Constants.GOOGLE_API_CLIENT_ERROR_MSG,
                connectionResult.getErrorCode()));
    }
}
 
Example #10
Source File: AttractionListFragment.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    Location location =
            intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);
    if (location != null) {
        mLatestLocation = new LatLng(location.getLatitude(), location.getLongitude());
        mAdapter.mAttractionList = loadAttractionsFromLocation(mLatestLocation);
        mAdapter.notifyDataSetChanged();
    }
}
 
Example #11
Source File: UtilityService.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
/**
 * Called when a location update is requested
 */
private void requestLocationInternal() {
    Log.v(TAG, ACTION_REQUEST_LOCATION);
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .build();

    // It's OK to use blockingConnect() here as we are running in an
    // IntentService that executes work on a separate (background) thread.
    ConnectionResult connectionResult = googleApiClient.blockingConnect(
            Constants.GOOGLE_API_CLIENT_TIMEOUT_S, TimeUnit.SECONDS);

    if (connectionResult.isSuccess() && googleApiClient.isConnected()) {

        Intent locationUpdatedIntent = new Intent(this, UtilityService.class);
        locationUpdatedIntent.setAction(ACTION_LOCATION_UPDATED);

        // Send last known location out first if available
        Location location = FusedLocationApi.getLastLocation(googleApiClient);
        if (location != null) {
            Intent lastLocationIntent = new Intent(locationUpdatedIntent);
            lastLocationIntent.putExtra(
                    FusedLocationProviderApi.KEY_LOCATION_CHANGED, location);
            startService(lastLocationIntent);
        }

        // Request new location
        LocationRequest mLocationRequest = new LocationRequest()
                .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        FusedLocationApi.requestLocationUpdates(
                googleApiClient, mLocationRequest,
                PendingIntent.getService(this, 0, locationUpdatedIntent, 0));

        googleApiClient.disconnect();
    } else {
        Log.e(TAG, String.format(Constants.GOOGLE_API_CLIENT_ERROR_MSG,
                connectionResult.getErrorCode()));
    }
}
 
Example #12
Source File: AttractionListFragment.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    Location location =
            intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);
    if (location != null) {
        mLatestLocation = new LatLng(location.getLatitude(), location.getLongitude());
        mAdapter.mAttractionList = loadAttractionsFromLocation(mLatestLocation);
        mAdapter.notifyDataSetChanged();
    }
}
 
Example #13
Source File: AndroidLocationPlayServiceManager.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
static android.location.Location extractLocationFromIntent(Intent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        LocationResult locationResult = LocationResult.extractResult(intent);
        if (locationResult == null) {
            return null;
        }
        return locationResult.getLastLocation();
    } else {
        return intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);
    }
}
 
Example #14
Source File: LocationProvider.java    From open-location-code with Apache License 2.0 5 votes vote down vote up
@AutoFactory
public LocationProvider(
        @Provided GoogleApiAvailability googleApiAvailability,
        @Provided GoogleApiClient googleApiClient,
        @Provided FusedLocationProviderApi fusedLocationProviderApi,
        @Provided LocationManager locationManager,
        @Provided SensorManager sensorManager,
        @Provided Display displayManager,
        Context context,
        LocationCallback locationCallback) {
    this.mGoogleApiAvailability = googleApiAvailability;
    this.mGoogleApiClient = googleApiClient;
    this.mFusedLocationProviderApi = fusedLocationProviderApi;
    this.mLocationManager = locationManager;
    this.mContext = context;
    this.mLocationCallback = locationCallback;
    this.mSensorManager = sensorManager;
    this.mDisplay = displayManager;
    mLocationRequest =
            LocationRequest.create()
                    .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                    .setInterval(INTERVAL_IN_MS)
                    .setFastestInterval(FASTEST_INTERVAL_IN_MS);
    mNetworkLocationListener = createLocationListener();
    mGpsLocationListener = createLocationListener();

    determineIfUsingGms();
    if (isUsingGms()) {
        mGoogleApiClient.registerConnectionCallbacks(this);
        mGoogleApiClient.registerConnectionFailedListener(this);
    }
}
 
Example #15
Source File: AttractionListFragment.java    From wear-os-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    Location location =
            intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);
    if (location != null) {
        mLatestLocation = new LatLng(location.getLatitude(), location.getLongitude());
        mAdapter.mAttractionList = loadAttractionsFromLocation(mLatestLocation);
        mAdapter.notifyDataSetChanged();
    }
}
 
Example #16
Source File: UtilityService.java    From wear-os-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Called when a location update is requested
 */
private void requestLocationInternal() {
    Log.v(TAG, ACTION_REQUEST_LOCATION);

    if (!Utils.checkFineLocationPermission(this)) {
        return;
    }

    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .build();

    // It's OK to use blockingConnect() here as we are running in an
    // IntentService that executes work on a separate (background) thread.
    ConnectionResult connectionResult = googleApiClient.blockingConnect(
            Constants.GOOGLE_API_CLIENT_TIMEOUT_S, TimeUnit.SECONDS);

    if (connectionResult.isSuccess() && googleApiClient.isConnected()) {

        Intent locationUpdatedIntent = new Intent(this, UtilityService.class);
        locationUpdatedIntent.setAction(ACTION_LOCATION_UPDATED);

        // Send last known location out first if available
        Location location = FusedLocationApi.getLastLocation(googleApiClient);
        if (location != null) {
            Intent lastLocationIntent = new Intent(locationUpdatedIntent);
            lastLocationIntent.putExtra(
                    FusedLocationProviderApi.KEY_LOCATION_CHANGED, location);
            startService(lastLocationIntent);
        }

        // Request new location
        LocationRequest mLocationRequest = new LocationRequest()
                .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        FusedLocationApi.requestLocationUpdates(
                googleApiClient, mLocationRequest,
                PendingIntent.getService(this, 0, locationUpdatedIntent, 0));

        googleApiClient.disconnect();
    } else {
        Log.e(TAG, String.format(Constants.GOOGLE_API_CLIENT_ERROR_MSG,
                connectionResult.getErrorCode()));
    }
}
 
Example #17
Source File: BackgroundLocationHandler.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    //String className = intent.getStringExtra("backgroundClass");
    final android.location.Location location = intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);
    if (AndroidLocationPlayServiceManager.inMemoryBackgroundLocationListener != null) {
        // This is basically just a short-term location request and we are using the in-memory listeners.
        AndroidLocationPlayServiceManager mgr = AndroidLocationPlayServiceManager.inMemoryBackgroundLocationListener;
        mgr.onLocationChanged(location);
        return;
    }
    if (intent.getDataString() == null) {
        System.out.println("BackgroundLocationHandler received update without data string.");
        return;
    }
    String[] params = intent.getDataString().split("[?]");
    

    //might happen on some occasions, no need to do anything.
    if (location == null) {
        return;
    }
    //if the Display is not initialized we need to launch the CodenameOneBackgroundLocationActivity 
    //activity to handle this
    boolean shouldStopWhenDone = false;
    if (!Display.isInitialized()) {
        shouldStopWhenDone = true;
        AndroidImplementation.startContext(this);
        //Display.init(this);
        /*
        Intent bgIntent = new Intent(getBaseContext(), CodenameOneBackgroundLocationActivity.class);
        Bundle b = new Bundle();
        b.putString("backgroundLocation", params[1]);
        b.putParcelable("Location", location);
        bgIntent.putExtras(b); //Put your id to your next Intent
        bgIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplication().startActivity(bgIntent);
                */
    } //else {

    try {
        //the 2nd parameter is the class name we need to create
        LocationListener l = (LocationListener) Class.forName(params[1]).newInstance();
        l.locationUpdated(AndroidLocationManager.convert(location));
    } catch (Exception e) {
        Log.e("Codename One", "background location error", e);
    }
    if (shouldStopWhenDone) {
        AndroidImplementation.stopContext(this);
    }
    //}
}
 
Example #18
Source File: GoogleApiModule.java    From open-location-code with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("SameReturnValue")
@Provides
@Singleton
public FusedLocationProviderApi provideFusedLocationProviderApi() {
    return LocationServices.FusedLocationApi;
}
 
Example #19
Source File: LocationProviderGmsCore.java    From 365browser with Apache License 2.0 4 votes vote down vote up
LocationProviderGmsCore(GoogleApiClient client, FusedLocationProviderApi locationApi) {
    mGoogleApiClient = client;
    mLocationProviderApi = locationApi;
}