2017-03-05 55 views
1

我已經在應用中實施了一些地理柵欄,並使用我的測試設備上的模擬位置(運行Android 5.0.1的三星S4)對它們進行了測試。在使用模擬位置時,轉換檢測一直在100%的時間內工作。我現在已經轉移到其他Android設備上,並將應用程序放置在經常(實際)進入並離開地理圍欄位置的某些電話上,並且我注意到檢測到OFTEN不起作用。這真的很不方便,所以我希望能有一些方法使檢測更加一致。要創建一致的Android地理柵欄監測

我的代碼調用地理圍欄:

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"); 

     } 
    } 
} 

我看到網上說用說的廣播接收機的工作比我更好過完成但令人尷尬的是,我不太瞭解如何將我所做的事轉化爲別人所建議的。

感謝。

回答