@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 onConnectionSuspended(int i) { Log.d(TAG, "LocationServices connection suspended"); state = State.SUSPENDED; cancelGeofencesFetching(); if (geofenceRequestIntent != null && geofenceList != null) { LocationServices.GeofencingApi.removeGeofences(apiClient, geofenceRequestIntent); } }
@Override public void onDestroy() { super.onDestroy(); if (mGoogleApiClient.isConnected()) { LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, getGeofencePendingIntent()) .setResultCallback(this); mGoogleApiClient.disconnect(); } if (!Storage.isEmptyLocations()) { sendGeofenceInfo(); } }
@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); }
/** * Removes geofences, which stops further notifications when the device enters or exits previously * registered geofences. */ public void removeGeofencesButtonHandler(View view) { if (!mGoogleApiClient.isConnected()) { Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show(); return; } try { // Remove geofences. LocationServices.GeofencingApi.removeGeofences( mGoogleApiClient, // This is the same pending intent that was used in addGeofences(). 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); } }
/** * 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); } }
// 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); } } }
@Override public void onConnectionSuspended(int i) { if (geofenceRequestIntent != null) LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, geofenceRequestIntent); onGeofencePreparedListener.onError(null, "onConnectionSuspended"); }
@Override public void onConnectionSuspended(int i) { if (mGeofencePendingIntent != null) { LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, mGeofencePendingIntent); } }