设置开始时间以监视地理围栏

时间:2018-07-17 18:32:24

标签: android duration android-geofence

根据此处的Google Geofence API示例

Geofencing Example Google API

添加地理围栏对象时,您可以设置expirationDurationTime。

如何设置在Android中启动地理围栏监视的时间? 示例:

我想在14:30到15:30在“ abc”位置以“ xyz”地理围栏进行明天的地理围栏监视 我没有找到Google geofence api提供的任何功能,可以设置开始监视geofence对象的时间。

请帮助我。我现在非常绝望:(

更新

从@Pavneet_Singh的帖子中了解到WorkManager之后 我仍然对如何在这里组合WorkManager感到困惑,因为我已经用IntentService扩展了我的类,以便从Location.Service接收意图。

公共类GeofenceTransitionsIntentService扩展了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;
}

}

我的想法: 从GeofenceTransitionsIntentService传递notificationMessage到另一个从Worker扩展的类,然后从那里使用setInitialdelay方法延迟推送通知?

我发现有人用JobIntentService扩展了GeofenceTransitionsIntentService类,但是我不认为我可以用JobIntentService扩展我的类,因为JobIntentService没有提供延迟功能。

我现在如何使它工作,我的想法正确吗?

谢谢

1 个答案:

答案 0 :(得分:1)

您实际上是在询问在某个特定时间触发的警报机制。 有很多选项可以做到这一点

最佳解决方案:

  • WorkManager
  • JobScheduler

其他(由于Naught,Oreo的背景限制)

  • 警报

通过上面的按钮,您可以在特定时间启动任务

参考文献:

Schedule a work on a specific time with WorkManager

how to do something at specific time for everyday in android