地理围栏不调用广播接收器,即使我强制初始触发



我正在尝试用GeoFence构建一个应用程序。我遵循以下文档:

https://developer.android.com/training/location/geofencing

我在坐标周围创建一个地理围栏,并指定

builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_EXIT);

所以,当地理围栏被创建时,应该触发一个ENTER或EXIT触发器(实际上它应该是一个ENTER,因为坐标是我在它上面的)。

我在broadcastReceiver上放了一个toast和一个断点,但是它没有触发。

这是我的代码:

清单:

<manifest [...]
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
[...]
<receiver
android:name=".GeofenceBroadcastReceiver"
android:enabled="true"
android:exported="true" />
</application>
</manifest>

MainActivity:

public class MainActivity extends AppCompatActivity {
private PendingIntent geofencePendingIntent;
private GeofencingClient geofencingClient;
@Override
protected void onCreate(Bundle savedInstanceState) {

[Not related stuff and foreground & background location permissions already granted]
geofencingClient = LocationServices.getGeofencingClient(this);
geofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
.addOnSuccessListener(this, new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity.this, "SUCCESFULLY CREATED GEOFENCES", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "ERROR CREATING GEOFENCES: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}

private PendingIntent getGeofencePendingIntent() {
// Reuse the PendingIntent if we already have it.
if (geofencePendingIntent != null) {
return geofencePendingIntent;
}
Intent intent = new Intent(this, GeofenceBroadcastReceiver.class);
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
// calling addGeofences() and removeGeofences().
geofencePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return geofencePendingIntent;
}

private GeofencingRequest getGeofencingRequest() {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_EXIT);
builder.addGeofences(Collections.singletonList(new Geofence.Builder()
// Set the request ID of the geofence. This is a string to identify this
// geofence.
.setRequestId("HOME")
.setCircularRegion(
123.456,
-123.456,
100000
)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_EXIT)
.build()));
return builder.build();
}

当我启动应用程序时,我得到"成功创建的地理围栏";Toast,但是intent永远不会被触发。

如果有一个围栏,我显然是在里面或外面,我想我应该得到一个事件,对吗?

还有,这里是广播接收器:

public class GeofenceBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); // There's a breakpoint here
if (geofencingEvent.hasError()) {
String errorMessage = GeofenceStatusCodes
.getStatusCodeString(geofencingEvent.getErrorCode());
Toast.makeText(context.getApplicationContext(), "error in broadcast receiver", Toast.LENGTH_SHORT);
return;
}
// Get the transition type.
int geofenceTransition = geofencingEvent.getGeofenceTransition();
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

Toast.makeText(context.getApplicationContext(), "EVENT FIRED:" + (geofenceTransition == 1 ? "ENTERED" : "EXITED"), Toast.LENGTH_SHORT).show();

} else {
Toast.makeText(context.getApplicationContext(), "NOT ENTER NOR EXIT", Toast.LENGTH_SHORT).show();
}

}

在我的例子中,过期持续时间为50秒。由于触发时间可能长达2-3分钟,因此在触发事件之前,地理围栏已经过期。

检查过期时间是否足够。

最新更新