2017-03-05 6 views
1

私はアプリにいくつかのジオフェンスを実装して、テストデバイス(Android 5.0.1を実行しているSamsung S4)のモックロケーションを使ってテストしています。モックロケーションを使用している間、トランジション検出は100%時間をかけて動作しています。私は今、他のAndroidデバイスに移動し、そのアプリケーションをジオフェンスの場所に頻繁に(物理的に)入退室する電話機に設置しました.OFTENの検出が機能しないことに気付きました。これは本当に不便なので、検出をより一貫させるための方法があることを期待していました。一貫性のあるAndroid Geofenceのモニタリング

ジオフェンスを作成する呼び出しを私のコード:

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

     } 
    } 
} 

私は他の人が何を示唆しているのかを本当にどのように変換するのかを理解するのに十分ではありません。

ありがとうございました。

答えて

関連する問題