android - Android to update geofence regularly
问题描述
I am reading through documentation from here.
https://developer.android.com/training/location/geofencing
It can update me location for my geofence when I run that service. Problem is I need to monitor continuously. So, I am thinking to do timer in my service and check my geofence regularly. But, I think it will definitely drain battery.
I don't want to monitor location and instead, I need android OS to update me whether user goes inside my geofence just like iOS. Is there any way to implement that ?
public class GeofenceTransitionsIntentService extends IntentService {
// ...
protected void onHandleIntent(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(
this,
geofenceTransition,
triggeringGeofences
);
// Send notification and log the transition details.
sendNotification(geofenceTransitionDetails);
Log.i(TAG, geofenceTransitionDetails);
} else {
// Log the error.
Log.e(TAG, getString(R.string.geofence_transition_invalid_type,
geofenceTransition));
}
}
解决方案
推荐阅读
- database - 具有不同产品颜色的多个页面
- javascript - Discord.js 男高音 gif 丰富的嵌入
- angular - 如何将原型分配给泛型类型
- python - 尝试按照教科书提示编写代码
- python - bt backtesting --> AttributeError: 'Series' object has no attribute 'columns'
- r - 如何使R根据标准从另一列中获取值?
- kubernetes - 将保险库后端与大三角帆集成时无法覆盖 defaultKey
- css - 在样式组件中调用函数
- spring-boot - 执行存储过程时如何在 Spring Data JPA 中实现乐观锁定?
- javascript - 为什么我的脚本有时有效,有时无效?谷歌脚本