android – 设置监控地理围栏的开始时间

根据此处的Google Geofence API示例

Geofencing Example Google API

您可以在添加地理栅栏对象时设置expirationDurationTime.

如何在Android中设置启动Geofence Monitoring的时间?
例:

我希望明天14:30到15:30在“abc”位置进行地理围栏监测,并进行“xyz”地理围栏循环
我没有找到谷歌地理围栏api提供的任何功能,我可以设置开始监控地理围栏对象的时间.

请帮我.我现在非常绝望:(

更新

从@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不提供延迟功能.

我怎么能让它现在起作用,我是否正确思考?

谢谢

最佳答案 您实际上是在询问在某个特定时间触发的警报机制.

有很多选项可以做到这一点

最优方案:

> WorkManager
> JobScheduler

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

>报警

有了上述功能,您可以在特定时间开始执行任务

参考文献:

Schedule a work on a specific time with WorkManager

how to do something at specific time for everyday in android

点赞