根据此处的 Google Geofence API 示例
您可以在添加地理围栏对象时设置 expirationDurationTime。
如何在 Android 中设置启动地理围栏监控的时间? 示例:
我想在明天 14:30 到 15:30 在“abc”位置使用“xyz”地理围栏周期进行地理围栏监控 我没有找到google geofence api提供的任何功能,我可以设置开始监视地理围栏对象的时间。
请帮帮我。我现在很绝望:(
更新
从@Pavneet_Singh 的帖子中阅读了有关 WorkManager 的信息后 我仍然对如何在此处组合 WorkManager 感到困惑,因为我已经使用 IntentService 扩展了我的类,以便获得从 Location.Service 接收到的 Intent 。
公共(public)类 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;
}
我的想法: 将 notificationMessage 从 GeofenceTransitionsIntentService 传递到另一个从 Worker 扩展的类,并从那里使用 setInitialdelay 方法延迟推送通知?
我发现有人用 JobIntentService 扩展了 GeofenceTransitionsIntentService 类,但我认为我不能用 JobIntentService 扩展我的类,因为 JobIntentService 不提供延迟功能。
我现在如何让它发挥作用,我的想法是否正确?
谢谢
最佳答案
您实际上是在请求在某个特定时间触发的警报机制。 有很多选项可以做到这一点
最优解:
- 工作经理
- 作业调度器
其他(由于Naught, Oreo 背景限制)
- 报警
有了上面你可以在某个特定的时间开始一个任务
引用资料:
Schedule a work on a specific time with WorkManager
how to do something at specific time for everyday in android
关于android - 设置监控地理围栏的开始时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51388181/