当我使用地理围栏时,进入或退出触发器不会被触发

Ruk*_*dhi 5 android google-maps-api-3 geofencing android-geofence

我正在我的 Android 应用程序中使用地理围栏,点击此链接

我正在默认可见活动中创建和添加地理围栏对象,并使用上面链接中所述的帮助 IntentService 监听其事件。

地理围栏对象已成功添加,但当我使用真实设备对其进行测试并移入/移出定义的地理围栏区域时,其 ENTER/EXIT 事件不会被触发。

我已经用 100m、50m、500m、1500m 和 2000m 的半径对其进行了测试,但在任何情况下它都不起作用。

这就是我创建和添加地理围栏的方式:

 mGeofencingClient = LocationServices.getGeofencingClient(DashboardActivity.this);

Geofence geofence = new Geofence.Builder()
                // Set the request ID of the geofence. This is a string to identify this
                // geofence.
                .setRequestId(REQUEST_ID)

                .setCircularRegion(
                        22.703617,
                        75.873409,
                        Constants.GEOFENCE_RADIUS_IN_METERS
                )
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
                        Geofence.GEOFENCE_TRANSITION_EXIT )

                .build();
        mGeofenceList.add(geofence);

        GeofencingRequest geofencingRequest = new GeofencingRequest.Builder()
                .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER|GeofencingRequest.INITIAL_TRIGGER_EXIT)
                .addGeofences(mGeofenceList).build();

        Intent intents = new Intent(this, GeofenceTransitionsIntentService.class);
        mGeofencePendingIntent = PendingIntent.getService(DashboardActivity.this, 0, intents, PendingIntent.
                FLAG_UPDATE_CURRENT);


        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            return;
        }else {
            mGeofencingClient.addGeofences(geofencingRequest, mGeofencePendingIntent)
                    .addOnSuccessListener(this, new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            Log.e("geofencing success", "successfully added");
                        }
                    }).addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.e("geofencing failure", "failed in added");

                }
            });
        }
Run Code Online (Sandbox Code Playgroud)

要监听 ENTER/EXIT 事件,我正在使用 IntentService,如下所示:

public class GeofenceTransitionsIntentService extends IntentService {

    public GeofenceTransitionsIntentService(String name) {
        super(name);
    }

    protected void onHandleIntent(Intent intent) {
        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
        Toast.makeText(this,"service get start",Toast.LENGTH_LONG).show();

        Log.e("service","geofencing service started");
        if (geofencingEvent.hasError()) {
            String errorMessage = GeofenceErrorMessages.getErrorString(this,
                    geofencingEvent.getErrorCode());
            Toast.makeText(this,"error_in_geofancing"+errorMessage,Toast.LENGTH_LONG).show();
            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 || geofenceTransition == Geofence.GEOFENCE_TRANSITION_DWELL) {

            // Get the geofences that were triggered. A single event can trigger
            // multiple geofences.
            List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

            Toast.makeText(this,"you are inside range",Toast.LENGTH_LONG).show();
            // Get the transition details as a String.
            String geofenceTransitionDetails = getGeofenceTransitionDetails(geofenceTransition, triggeringGeofences);

            // Send notification and log the transition details.
          sendNotification(geofenceTransitionDetails);
            Log.e("geofancing","you are inside range");

        } else {
            Toast.makeText(this,"Invalid_Transition",Toast.LENGTH_LONG).show();
        }
    }
    private String getGeofenceTransitionDetails(
           // Context context,
            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;
    }
    }
Run Code Online (Sandbox Code Playgroud)

我为此添加了 ACCESS_FINE_LOCATION 和 LOCATION_HARDWARE 的权限。