[英]Geofence not calling broadcast receiver even if I enforce the inital trigger
I'm trying to build an application with GeoFence.我正在尝试使用 GeoFence 构建应用程序。 I followed this documentation:
我遵循了这个文档:
https://developer.android.com/training/location/geofencing https://developer.android.com/training/location/geofencing
I'm creating a geofence around my coordinates and specifying我正在围绕我的坐标创建一个地理围栏并指定
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_EXIT);
So, when the geofence is created should trigger an ENTER or an EXIT trigger (actually it should be an ENTER as the coordinates are the ones I'm on it).因此,当创建地理围栏时应该触发 ENTER 或 EXIT 触发器(实际上它应该是 ENTER 因为坐标是我在上面的坐标)。
I put a toast on the broadcastReceiver and also a breakpoint but it didn't fire.我在 broadcastReceiver 和断点上敬酒,但它没有触发。
This is my code:这是我的代码:
Manifest:显现:
<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();
}
When I start the application I get the "SUCCESFULLY CREATED GEOFENCES" toast, but the intent is never triggered.当我启动应用程序时,我得到了“SUCCESFULLY CREATED GEOFENCES”吐司,但从未触发意图。
If there is a goefence, and I am obviusly inside or outside, I should get an event, i guess, right?如果有围栏,而我显然在里面或外面,我想我应该得到一个事件,对吧?
Also, here it is the broadcast receiver:此外,这是广播接收器:
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();
}
}
In my case the expiration duration was 50 seconds.在我的例子中,过期时间是 50 秒。 As the trigger could take upto 2-3 minutes , geofence was expiring before event triggered.
由于触发器最多可能需要 2-3 分钟,因此地理围栏在事件触发之前就已过期。
Check expiration duration is enough.检查过期时间是否足够。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.