设备进入位置时不会触发地理围栏

时间:2019-05-01 08:35:48

标签: android android-geofence

我正在尝试创建地理围栏,然后在用户输入或退出位置时做一些事情(例如显示通知,静音移动设备)。

遵循本指南https://developer.android.com/training/location/geofencing.html#HandleGeofenceTransitions

尽管正在创建地理围栏,但是当设备进入位置(控件不会转到GeofenceTrasition.class)并且即使它执行了geoFenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER都不会执行时,什么也不会发生

我已经检查了先前的问题和指南,但是没有任何帮助,我也遍历了github(无法正确理解该代码)

地理围栏的MapsActivity代码

private 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 个答案:

答案 0 :(得分:0)

我很笨,所以我将地理围栏的经度和纬度设置为: .setCircularRegion(address.getLongitude(),address.getLatitude(),GEOFENCE_RADIUS_IN_METERS)

虽然第一个值表示纬度,第二个值表示经度,但正确的蕴含量为: .setCircularRegion(address.getLatitude(),address.getLongitude(),GEOFENCE_RADIUS_IN_METERS)