@Override
 public void onConnected(Bundle bundle) {
   Log.d("GEO", "Location client connected");
   if (mAction == Action.ADD) {
     GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
     Log.d("GEO", "Location client adds geofence");
     builder.setInitialTrigger(
         transitionType == Geofence.GEOFENCE_TRANSITION_ENTER
             ? GeofencingRequest.INITIAL_TRIGGER_ENTER
             : GeofencingRequest.INITIAL_TRIGGER_EXIT);
     builder.addGeofences(mGeofenceListsToAdd);
     GeofencingRequest build = builder.build();
     LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, build, getPendingIntent())
         .setResultCallback(
             new ResultCallback<Status>() {
               @Override
               public void onResult(@NonNull Status status) {
                 if (status.isSuccess()) {
                   String msg = "Geofences added: " + status.getStatusMessage();
                   Log.e("GEO", msg);
                   Toast.makeText(GeofencingService.this, msg, Toast.LENGTH_SHORT).show();
                 }
                 GeofencingService.this.onResult(status);
               }
             });
   }
 }
  @Override
  public void onConnected(Bundle bundle) {
    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

    if (location == null) {
      LocationServices.FusedLocationApi.requestLocationUpdates(
          mGoogleApiClient, mLocationRequest, this);
    } else {
      handleNewLocation(location);
    }

    LocationServices.GeofencingApi.addGeofences(
            mGoogleApiClient, getGeofencingRequest(), getGeofencePendingIntent())
        .setResultCallback(this);
  }
  /**
   * Adds geofences, which sets alerts to be notified when the device enters or exits one of the
   * specified geofences. Handles the success or failure results returned by addGeofences().
   */
  public void addGeofencesButtonHandler(View view) {
    if (!mGoogleApiClient.isConnected()) {
      Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show();
      return;
    }

    try {
      LocationServices.GeofencingApi.addGeofences(
              mGoogleApiClient,
              // The GeofenceRequest object.
              getGeofencingRequest(),
              // A pending intent that that is reused when calling removeGeofences(). This
              // pending intent is used to generate an intent when a matched geofence
              // transition is observed.
              getGeofencePendingIntent())
          .setResultCallback(this); // Result processed in onResult().
    } catch (SecurityException securityException) {
      // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission.
      logSecurityException(securityException);
    }
  }
Esempio n. 4
0
  // onClick handler for when they're ready to use our app
  public void onReadyToStart(View view) {

    // add a confirmation for the user
    if (!readyClicked) {
      TextView readyView = new TextView(getApplicationContext());
      readyView.setBackgroundResource(R.drawable.border);
      readyView.setTextColor(getResources().getColor(android.R.color.holo_green_light));
      readyView.setText("Your text will be sent when you arrive at your location :)");
      readyView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
      LinearLayout rootLinearLayout = (LinearLayout) findViewById(R.id.topLinearLayout);

      rootLinearLayout.addView(readyView);

      // set the ready button to green
      Button readyBtn = (Button) findViewById(R.id.startButtonID);
      readyBtn.setTextColor(getResources().getColor(android.R.color.holo_green_light));
      readyClicked = true;
    }

    if (mGoogleApiClient.isConnected()) {
      Geofences geofences = new Geofences(lat, lon, radius, expiration);

      mGeofencePendingIntent = getGeofenceTransitionPendingIntent();
      if (ContextCompat.checkSelfPermission(
                  getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION)
              == PackageManager.PERMISSION_GRANTED
          || ContextCompat.checkSelfPermission(
                  getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION)
              == PackageManager.PERMISSION_GRANTED) {
        LocationServices.GeofencingApi.addGeofences(
            mGoogleApiClient, Geofences.geofencingRequest, mGeofencePendingIntent);
        // below is deprecated
        // LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, Geofences.geoFenceList,
        // mGeofencePendingIntent);
      }
    }
  }