一致的Android地理围栏监控

时间:2017-03-05 15:03:29

标签: android android-geofence

我已经在应用程序中实现了一些地理围栏,并且已经在我的测试设备(运行Android 5.0.1的Samsung S4)上使用模拟位置测试它们。在使用模拟位置时,转换检测一直在100%的时间内工作。我现在转移到其他Android设备上并将应用程序放在一些经常(物理上)进入和离开地理围栏位置的手机上,我注意到检测到OFTEN不起作用。这真的很不方便,所以我希望有一些方法可以使检测更加一致。

我的代码调用了地理围栏:

private void startGeofenceMonitoring() {
    Log.d(TAG, "startGeofenceMonitoring called");
    try {
        Geofence geofence = new Geofence.Builder()
                .setRequestId(GEOFENCE_ID)
                .setCircularRegion(51.364516, -0.189643, 150)
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .setNotificationResponsiveness(1000)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                .build();

        GeofencingRequest geofencingRequest = new GeofencingRequest.Builder()
                .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_EXIT)
                .addGeofence(geofence)
                .build();

        Intent intent = new Intent(this, GeofenceService.class);
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        if (!googleApiClient.isConnected()) {
            Log.d(TAG, "GoogleApiClient is not connected");
        } else {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                ActivityCompat.requestPermissions(MapsActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION);

                return;
            }
            LocationServices.GeofencingApi.addGeofences(googleApiClient, geofencingRequest, pendingIntent)
                    .setResultCallback(new ResultCallback<Status>() {
                        @Override
                        public void onResult(@NonNull Status status) {
                            if (status.isSuccess()) {
                                Log.d(TAG, "Successfully added Geofence");
                            } else {
                                Log.d(TAG, "Failed to add geofence - " + status.getStatus());
                            }
                        }
                    });
        }
    } catch (SecurityException e) {
        Log.d(TAG, "SecurityException - " + e.getMessage());
    }
} 

GeofenceService.java:

public class GeofenceService extends IntentService {

public static final String TAG = "GeofenceService";

DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();

public GeofenceService() {
    super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent event = GeofencingEvent.fromIntent(intent);
    if (event.hasError()) {
        //TODO:
    } else {

        double longitude = 0;
        double latitude = 0;

        int transition = event.getGeofenceTransition();
        List<Geofence> geofences = event.getTriggeringGeofences();
        Geofence geofence = geofences.get(0);
        String requestID = geofence.getRequestId();

        longitude = event.getTriggeringLocation().getLongitude();
        latitude = event.getTriggeringLocation().getLatitude();

        final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

        if (longitude != 0 && latitude != 0) {

            if (transition == Geofence.GEOFENCE_TRANSITION_ENTER) {
                Log.d(TAG, "Entering geofence - " + requestID);

                //My on enter code

            } else if (transition == Geofence.GEOFENCE_TRANSITION_EXIT) {
                Log.d(TAG, "Exited geofence - " + requestID);
                //My on exit code
            }
        } else {

            mRootRef.child("users/" + user.getUid() + "/error").setValue("lat/long = 0");

        }
    }
}

我在网上看到,使用广播接收器的效果比我所做的要好但令人尴尬的是,我还不够好,不能真正理解如何将我所做的转换为什么其他人建议。

感谢。

1 个答案:

答案 0 :(得分:0)

查看这个答案,有一个带地理围栏的广播接收器的实现

Android Geofencing Notifications when app is closed