4

According to Google Geofence API example here

Geofencing Example Google API

you can set the expirationDurationTime, when you add a geofence object.

How can I set the time for start Geofence Monitoring in Android ? Example:

I want to have a geofence monitoring for tomorrow at 14:30 til 15:30 in "abc" location with "xyz" geofence cycle I did not found any function provide by google geofence api, which i can set the time of start monitoring for a geofence object.

Please help me. I am very desperate right now :(

Update

after reading about WorkManager from post of @Pavneet_Singh I am still confuse how to combine the WorkManager here, because i have already extends my class with IntentService in order to get the intent received from Location.Service.

public class GeofenceTransitionsIntentService extends IntentService {

private final String TAG = "GeofenceTransIntServ";

/**
 * Creates an IntentService.  Invoked by your subclass's constructor.
 *
 * @param name Used to name the worker thread, important only for debugging.
 */
public GeofenceTransitionsIntentService(String name) {
    super(name);
}


/**
 * This method is invoked on the worker thread with a request to process.
 * Only one Intent is processed at a time, but the processing happens on a
 * worker thread that runs independently from other application logic.
 * So, if this code takes a long time, it will hold up other requests to
 * the same IntentService, but it will not hold up anything else.
 * When all requests have been handled, the IntentService stops itself,
 * so you should not call {@link #stopSelf}.
 *
 * @param intent The value passed to {@link
 *               Context#startService(Intent)}.
 *               This may be null if the service is being restarted after
 *               its process has gone away; see
 *               {@link Service#onStartCommand}
 *               for details.
 */

@Override
protected void onHandleIntent(@Nullable Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        String errorMessage = GeofenceErrorMessages.getErrorString(this,
                geofencingEvent.getErrorCode());
        Log.e(TAG, errorMessage);
        return;
    }
    // Get the transition type.
    int geofenceTransition = geofencingEvent.getGeofenceTransition();

    // Test that the reported transition was of interest.
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
            geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

        // Get the geofences that were triggered. A single event can trigger
        // multiple geofences.
        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

        // Get the transition details as a String.
        String geofenceTransitionDetails = getGeofenceTransitionDetails(
                geofenceTransition,
                triggeringGeofences
        );

        // Send notification and log the transition details.
        sendNotification(geofenceTransitionDetails);
        Log.i(TAG, geofenceTransitionDetails);
    } else {
    // Error
    }

}

private void sendNotification(String geofenceTransitionDetails) {
    // send notification
}


/**
 * Gets transition details and returns them as a formatted string.
 *
 * @param geofenceTransition    The ID of the geofence transition.
 * @param triggeringGeofences   The geofence(s) triggered.
 * @return                      The transition details formatted as String.
 */
private String getGeofenceTransitionDetails(
        int geofenceTransition,
        List<Geofence> triggeringGeofences) {

    String geofenceTransitionString = getTransitionString(geofenceTransition);

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

    return geofenceTransitionString + ": " + triggeringGeofencesIdsString;
}

private String getTransitionString(int geofenceTransition) {
    // get the transitionType as String
    return null;
}

}

My Idea : Pass notificationMessage from GeofenceTransitionsIntentService to another class which extends from Worker and from there use the setInitialdelay method to push the notification with delay ?

I found someone extends the GeofenceTransitionsIntentService class with JobIntentService but i do not think i can extends my Class with JobIntentService because JobIntentService does´nt offer a delay function.

How could I get it work now and am I right with my thinking ?

Thanks

user7112196
  • 87
  • 2
  • 9

1 Answers1

1

You are actually asking for Alarm mechanism which triggers at some particular time. There are many option available to do this

Optimal solutions:

  • WorkManager
  • JobScheduler

Other (Due to background restriction in Naught, Oreo )

  • Alarm

With above you can start a task at some particular time

References:

Schedule a work on a specific time with WorkManager

how to do something at specific time for everyday in android

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68