/**
   * Handles incoming intents
   *
   * @param intent The Intent sent by Location Services. This Intent is provided to Location
   *     Services (inside a PendingIntent) when you call addGeofences()
   */
  @Override
  protected void onHandleIntent(Intent intent) {
    // First check for errors
    Log.i(TAG, "onHandleIntent");
    if (LocationClient.hasError(intent)) {
      // Get the error code with a static method
      int errorCode = LocationClient.getErrorCode(intent);
      // Log the error
      Log.e(
          "ReceiveGeofenceTransitionsIntentService",
          "Location Services error: " + Integer.toString(errorCode));
      /*
       * You can also send the error code to an Activity or
       * Fragment with a broadcast Intent
       */
      /*
       * If there's no error, get the transition type and the IDs
       * of the geofence or geofences that triggered the transition
       */
    } else {
      // Get the type of transition (entry or exit)
      int transitionType = LocationClient.getGeofenceTransition(intent);
      // Test that a valid transition was reported
      if ((transitionType == Geofence.GEOFENCE_TRANSITION_ENTER)
          || (transitionType == Geofence.GEOFENCE_TRANSITION_EXIT)) {
        List<Geofence> triggerList = LocationClient.getTriggeringGeofences(intent);

        String[] triggerIds = new String[triggerList.size()];

        for (int i = 0; i < triggerIds.length; i++) {
          // Store the Id of each geofence
          triggerIds[i] = triggerList.get(i).getRequestId();
          Log.i(TAG, "triggerId=" + triggerIds[i]);
        }

        Log.i(TAG, "transitionType=" + transitionType);

        for (Geofence geofence : triggerList) {
          generateNotification(geofence.getRequestId(), "address you defined");
        }

        // Toast.makeText(getApplicationContext(), "Transition Type = "+transitionType,
        // Toast.LENGTH_SHORT).show();
        /*
         * At this point, you can store the IDs for further use
         * display them, or display the details associated with
         * them.
         */
      }
      // An invalid transition was reported
      else {
        Log.e(
            "ReceiveGeofenceTransitionsIntentService",
            "Geofence transition error: " + Integer.toString(transitionType));
      }
    }
  }
  /**
   * Gets transition details and returns them as a formatted string.
   *
   * @param context The app context.
   * @param geofenceTransition The ID of the geofence transition.
   * @param triggeringGeofences The geofence(s) triggered.
   * @return The transition details formatted as String.
   */
  private String getGeofenceTransitionDetails(
      Context context, int geofenceTransition, List<Geofence> triggeringGeofences) {

    String geofenceTransitionString = getTransitionString(geofenceTransition);

    // Get the Ids of each geofence that was triggered.
    ArrayList triggeringGeofencesIdsList = new ArrayList();
    for (Geofence geofence : triggeringGeofences) {
      triggeringGeofencesIdsList.add(geofence.getRequestId());
    }
    String triggeringGeofencesIdsString = TextUtils.join(", ", triggeringGeofencesIdsList);

    return geofenceTransitionString + ": " + triggeringGeofencesIdsString;
  }
 @Override
 public void onReceive(Context context, Intent intent) {
   playerController =
       (PlayerController)
           context
               .getApplicationContext()
               .getSystemService(ActiveMtlApplication.PLAYER_CONTROLLER);
   List<Geofence> geofences = LocationClient.getTriggeringGeofences(intent);
   int transition = LocationClient.getGeofenceTransition(intent);
   Log.i(
       GeofenceTransitionReceiver.class.getSimpleName(),
       "onReceive transition : "
           + (transition == Geofence.GEOFENCE_TRANSITION_ENTER ? "Enter" : "Exit"));
   String title = "";
   String content = "";
   Builder builder =
       new Notification.Builder(context)
           .setSmallIcon(R.drawable.icon)
           .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon))
           .setDefaults(Notification.DEFAULT_ALL)
           .setContentIntent(HomeActivity.newPendingIntent(context, mId))
           .setAutoCancel(true);
   NotificationManager mNotificationManager =
       (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
   if (transition == Geofence.GEOFENCE_TRANSITION_ENTER
       && !PreferenceHelper.isCheckedIn(context)) {
     title = context.getString(R.string.entering_court_zone);
     content = context.getString(R.string.you_just_entered_a_court);
     for (Geofence geofence : geofences) {
       playerController.checkInCourt(geofence.getRequestId(), addPlayerCallback);
     }
     PreferenceHelper.checkIn(context);
     builder.setContentTitle(title).setContentText(content);
     mNotificationManager.notify(mId, builder.build());
   } else if (transition == Geofence.GEOFENCE_TRANSITION_EXIT
       && PreferenceHelper.isCheckedIn(context)) {
     title = context.getString(R.string.leaving_court_zone);
     content = context.getString(R.string.you_just_leaved_a_court);
     for (Geofence geofence : geofences) {
       playerController.leaveCourt(geofence.getRequestId(), deletePlayerCallback);
     }
     PreferenceHelper.leaveCourt(context);
     builder.setContentTitle(title).setContentText(content);
     mNotificationManager.notify(mId, builder.build());
   }
   // mId allows you to update the notification later on.
 }
  /**
   * Play music and show marker when in specific geofence.
   *
   * @param intent the current intent from PendingIntent; this Intent is received by current
   *     service, which contains geofencing information.
   */
  @Override
  protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
      Log.e(Constants.LOG_TAG, geofencingEvent.getErrorCode() + "");
      return;
    }

    // Get the transition type.
    int geofenceTransition = geofencingEvent.getGeofenceTransition();
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
      // Get the geofences that were triggered.
      // A single event can trigger multiple geofences.
      for (Geofence geofence : geofencingEvent.getTriggeringGeofences())
        Log.v(Constants.LOG_TAG, geofence.getRequestId());
      Geofence triggered = geofencingEvent.getTriggeringGeofences().get(0);

      // Log the transition details.
      Log.v(Constants.LOG_TAG, "Detect entering: " + triggered.getRequestId());
      Log.v(
          Constants.LOG_TAG,
          "Detect location: "
              + geofencingEvent.getTriggeringLocation().getLatitude()
              + ", "
              + geofencingEvent.getTriggeringLocation().getLongitude());
      // Play specific music according to location
      GeofenceMedia.getInstance().playSong(this, triggered.getRequestId());
      // Show marker on the location?
      GeofenceMedia.getInstance()
          .showMarker(geofencingEvent.getTriggeringLocation(), triggered.getRequestId());
    } else if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
      GeofenceMedia.getInstance().pauseSong();
      GeofenceMedia.getInstance().removeMarkers();
    } else {
      // Log the error.
      Log.e(Constants.LOG_TAG, "No Enter or Exit detected");
    }
  }
Example #5
0
 public boolean isGeofence(Geofence testGeofence) {
   return testGeofence.getRequestId().equals(requestId);
 }