设备进入某个位置时没有触发地理围栏
Posted
技术标签:
【中文标题】设备进入某个位置时没有触发地理围栏【英文标题】:Geofence isn't triggering when device enters a location 【发布时间】:2019-09-19 20:43:03 【问题描述】:我正在尝试创建地理围栏,然后在用户进入或退出某个位置时执行一些操作(例如显示通知、静音移动)。
按照本指南https://developer.android.com/training/location/geofencing.html#HandleGeofenceTransitions
虽然正在创建地理围栏,但当设备进入该位置时没有任何反应(控制不会转到 GeofenceTrasition.class),即使它执行了 geoFenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER 也不会执行
我检查了以前的问题和指南,但没有任何帮助我也浏览了 github(不正确理解该代码)
地理围栏的 MapsActivity 代码
私有 void buildGeofence() Log.d(TAG, "buildGeofence: BuilderExecuted");
mGeofenceList.add(new Geofence.Builder()
.setRequestId("A1")
.setCircularRegion(address.getLongitude(), address.getLatitude(), GEOFENCE_RADIUS_IN_METERS)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.build());
System.out.println("Longitude is " + address.getLongitude() + "Latitude is" + address.getLatitude());
mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
.addOnSuccessListener(new OnSuccessListener<Void>()
@Override
public void onSuccess(Void aVoid)
Log.d(TAG, "onSuccess: GeoFencing Successful");
)
.addOnFailureListener(new OnFailureListener()
@Override
public void onFailure(@NonNull Exception e)
Log.d(TAG, "onFailure: GeoFencing UnSuccessful");
);
Circle geofenceLimits;
public void addGeofenceCircle()
CircleOptions circleOptions = new CircleOptions()
.center(m1.getPosition())
.strokeColor(Color.argb(50,70,70,70))
.fillColor(Color.argb(100,150,150,150))
.radius(GEOFENCE_RADIUS_IN_METERS);
geofenceLimits = mMap.addCircle(circleOptions);
private GeofencingRequest getGeofencingRequest()
Log.d(TAG, "getGeofencingRequest: Geofencing request executed");
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofences(mGeofenceList);
return builder.build();
private PendingIntent getGeofencePendingIntent()
if (mGeofencePendingIntent != null)
return mGeofencePendingIntent;
Log.d(TAG, "getGeofencePendingIntent: Geofencing intenet executed");
Intent Intent = new Intent(this, GeofenceTransition.class);
mGeofencePendingIntent = PendingIntent.getService(MapsActivity.this, 0, Intent, PendingIntent.
FLAG_UPDATE_CURRENT);
return mGeofencePendingIntent;
类来处理意图并判断设备是在地理围栏内部还是外部 公共类 GeofenceTransition 扩展 IntentService
private static final String TAG = "GeofenceIntent";
public static final int GEOFENCE_NOTIFICATION_ID = 0;
public GeofenceTransition()
super("GeofenceTransition");
@Override
protected void onHandleIntent(Intent intent)
Log.d(TAG, "onHandleIntent: Executed");
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError())
String error = String.valueOf(geofencingEvent.getErrorCode());
Toast.makeText(getApplicationContext(), "Error = " + error, Toast.LENGTH_SHORT).show();
Log.e(TAG, "onHandleIntent: Intent Error");
return;
int geoFenceTransition = geofencingEvent.getGeofenceTransition();
int i=geoFenceTransition;
System.out.println(i);
System.out.println("Checking Geofence Transition");
if (geoFenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geoFenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT)
Log.d(TAG, "onHandleWork: Enter/Exit Works");
Toast.makeText(getApplicationContext(), "WORKING", Toast.LENGTH_SHORT).show();
【问题讨论】:
【参考方案1】:我很笨,所以我将地理围栏的经度和纬度设置为: .setCircularRegion(address.getLongitude(), address.getLatitude(), GEOFENCE_RADIUS_IN_METERS)
虽然第一个值是纬度,第二个是经度,但正确的实现应该是: .setCircularRegion(address.getLatitude(), address.getLongitude(), GEOFENCE_RADIUS_IN_METERS)
【讨论】:
以上是关于设备进入某个位置时没有触发地理围栏的主要内容,如果未能解决你的问题,请参考以下文章
Android 地理围栏在进入/退出时触发,没有错误但没有 GEOFENCE_TRANSITION_ 标志