2017-02-23 10 views
1

私は、外部APIからロードされたジオフェンスロケーションでAndroidアプリケーションを作成しようとしています。私は改造を使用してaysnc呼び出しを行います。問題は、googleApiClientと外部API呼び出しの両方が非同期であることです。だから私はジオフェンスを始めるためにどれが最初に終わるのかわからない。ジオフェンスを使用して複数のコールバックレベルを避ける方法

googleApiClientonConnected()のジオフェンスを開始すると、まだAPIのLatLngがない可能性があります。しかし、APIのコールバックからジオフェンスを開始すると、googleApiClientがまだロードされていない可能性があります。

googleApiClientonConnected()で非同期API呼び出しを行う代わりに、この問題を処理するにはどうすればよいですか。私は複数のコールバックレベルを避けようとしています。私はstartGeofences()が呼び出されたときにAPIの結果がまだ存在していないと思うので、ここでは、現在動作していない私のコードは次のとおりです。

public class GeofenceHelper implements GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, LocationListener, ResultCallback<Status> { 
    private List<Geofence> mGeofenceList = new ArrayList<>(); 
    private GoogleApiClient googleApiClient; 

public GeofenceHelper(Activity context){ 
     this.context = context; 
     permissionsHelper = new PermissionsHelper(context, REQ_PERMISSION); 
     buildGoogleApiClient(); 
     geofencePointsRequest(); 
    } 

private void startGeofences() { 
     Log.i(TAG, "startGeofences()"); 
     if (!googleApiClient.isConnected()) { 
      Log.d(TAG, "Not connected"); 
      return; 
     } 
     if (permissionsHelper.checkPermission()) 
      LocationServices.GeofencingApi.addGeofences(
        googleApiClient, 
        getGeofencingRequest(), 
        getGeofencePendingIntent() 
      ).setResultCallback(this); // Result processed in onResult() 
    } 

private void geofencePointsRequest() { 
    GeofenceAreasRequest response = new GeofenceAreasRequest(); 
    response.getAllAreas(new GeofenceAreasResponse() { 
     @Override 
     public void onAreasLoaded(List<Point> points, int code) { 
      Log.i(TAG, "Responsecode: " + String.valueOf(code)); 
      for (int i = 0; i < points.size(); i++) { 
       mGeofenceList.add(new Geofence.Builder() 
         .setRequestId(points.get(i).getName()) 
         .setCircularRegion(
           points.get(i).getLatitude(), 
           points.get(i).getLongitude(), 
           points.get(i).getRadius()) 
         .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER 
           | Geofence.GEOFENCE_TRANSITION_EXIT) 
         .setExpirationDuration(Geofence.NEVER_EXPIRE) 
         .build()); 
      } 
     } 
    }); 
} 


private GeofencingRequest getGeofencingRequest() { 
    Log.d(TAG, "getGeofencingRequest"); 
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); 
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); 
    builder.addGeofences(mGeofenceList); 
    return builder.build(); 
} 

public void start(){ 
    googleApiClient.connect(); 
} 
public void stop(){ 
    googleApiClient.disconnect(); 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    Log.i(TAG, "google api connected"); 
    startGeofences(); 
    getLastKnownLocation(); 
} 
} 
+0

てみてください、この方法を試してみてくださいあなたが呼出しの応答を受け取ったら、startGeofences()メソッドを呼び出すことで、aSncコールをonConnected()メソッドで呼び出すことができます。 – Umar

+0

あなたは精緻化できますか?元の投稿に説明されているように私が避けようとしていることではないからですか? – Tim

答えて

1

public class GeofenceHelper implements GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, LocationListener, ResultCallback<Status> { 
private List<Geofence> mGeofenceList = new ArrayList<>(); 
private GoogleApiClient googleApiClient; 


private List<Point> pointsList = null; 

public GeofenceHelper(Activity context) { 
    this.context = context; 
    permissionsHelper = new PermissionsHelper(context, REQ_PERMISSION); 

    // let both work in parallel 
    buildGoogleApiClient(); 
    geofencePointsRequest(); 
} 

private void startGeofences() { 
    Log.i(TAG, "startGeofences()"); 
    if (!googleApiClient.isConnected()) { 
     Log.d(TAG, "Not connected"); 
     return; 
    } 
    if (permissionsHelper.checkPermission()) 
     LocationServices.GeofencingApi.addGeofences(
       googleApiClient, 
       getGeofencingRequest(), 
       getGeofencePendingIntent() 
     ).setResultCallback(this); // Result processed in onResult() 
} 

private void registerGeofences() { 

    if (pointsList != null) { 
     // populate data in list 
     for (int i = 0; i < pointsList.size(); i++) { 
      mGeofenceList.add(new Geofence.Builder() 
        .setRequestId(pointsList.get(i).getName()) 
        .setCircularRegion(
          pointsList.get(i).getLatitude(), 
          pointsList.get(i).getLongitude(), 
          pointsList.get(i).getRadius()) 
        .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER 
          | Geofence.GEOFENCE_TRANSITION_EXIT) 
        .setExpirationDuration(Geofence.NEVER_EXPIRE) 
        .build()); 
     } 

     // this will actually register geofences 
     startGeofences(); 

    } 
} 

private void geofencePointsRequest() { 
    GeofenceAreasRequest response = new GeofenceAreasRequest(); 
    response.getAllAreas(new GeofenceAreasResponse() { 
     @Override 
     public void onAreasLoaded(List<Point> points, int code) { 
      Log.i(TAG, "Responsecode: " + String.valueOf(code)); 

      pointsList = points; 

      registerGeofences(); 
     } 
    }); 
} 


private GeofencingRequest getGeofencingRequest() { 
    Log.d(TAG, "getGeofencingRequest"); 
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); 
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); 
    builder.addGeofences(mGeofenceList); 
    return builder.build(); 
} 

public void start() { 
    googleApiClient.connect(); 
} 

public void stop() { 
    googleApiClient.disconnect(); 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    Log.i(TAG, "google api connected"); 
    getLastKnownLocation(); 
    registerGeofences(); 
} 

}

+0

ありがとう、それは今働いている – Tim

関連する問題