-1

Ich verwende GoogleApiClient, um den Benutzerstandort abzurufen. Ich habe eine Singleton-Klasse erstellt, um den Benutzerstandort zu ermitteln. Hier ist die Klasse:Wie gibt man den Wert auf GoogleApiClient onConnected() zurück?

public class LocationUtils { 

    private static LocationUtils locationUtils; 
    private GoogleApiClient googleApiClient; 

    public static LocationUtils getInstance() { 

     if(locationUtils == null) 
      locationUtils = new LocationUtils(); 
     return locationUtils; 
    } 

    private LocationUtils() {} 

    public Location getLocation(Context context){ 

     final Location[] location = {null}; 

     googleApiClient = new GoogleApiClient.Builder(App.getContext()) 
       .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { 
        @Override 
        public void onConnected(Bundle bundle) { 
         if(ContextCompat.checkSelfPermission(App.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) 
          location[0] = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 

         googleApiClient.disconnect(); 
        } 

        @Override 
        public void onConnectionSuspended(int i) { 

        } 
       }) 
       .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() { 
        @Override 
        public void onConnectionFailed(ConnectionResult connectionResult) { 

        } 
       }) 
       .addApi(LocationServices.API) 
       .build(); 

     googleApiClient.connect(); 

     return location[0]; 

    } 



} 

Ich möchte Benutzer Lage bekommen mit LocationUtils.getInstance() getLocation (Kontext).. Wenn ich die Methode getLocation() aufruft, sollte sie sich mit GoogleApiClient verbinden, den Benutzerstandort zurückgeben und die Verbindung zu GoogleApiClient für jeden Anruf trennen. Aber wenn ich diese Methode anrufe, gibt es null zurück. Der Hauptthread wartet nicht auf die Methode onConnected() und gibt das Positionsobjekt als null zurück. Wie kann es auf die Methode onConnect() warten? ODER wie kann ich Location-Objekt in OnConnected() zurückgeben?

GELÖST nach Tim Castelijns Antwort

Zuerst habe ich erstellt eine Schnittstelle:

public interface LocationCallbackInterface { 
    void onComplete(Location location); 
} 

dann verwendet es in getLocationMethod():

public void getLocation (Context Kontext, final LocationCallbackInterface Rückruf) {

final Location[] location = {null}; 

googleApiClient = new GoogleApiClient.Builder(App.getContext()) 
     .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { 
      @Override 
      public void onConnected(Bundle bundle) { 
       if(ContextCompat.checkSelfPermission(App.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
        location[0] = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
        //Toast.makeText(App.getContext(), "location came :" + location[0].toString(), Toast.LENGTH_LONG).show(); 
        callback.onComplete(location[0]); 
       } 

       googleApiClient.disconnect(); 
      } 

      @Override 
      public void onConnectionSuspended(int i) { 

      } 
     }) 
     .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() { 
      @Override 
      public void onConnectionFailed(ConnectionResult connectionResult) { 

      } 
     }) 
     .addApi(LocationServices.API) 
     .build(); 

googleApiClient.connect(); 

}

rief dann diese Methode in einer Aktivität:

LocationUtils.getInstance().getLocation(getActivity(), new LocationCallbackInterface() { 

      @Override 
      public void onComplete(Location location) { 
       if (location != null) { 
        Toast.makeText(getActivity(), "location :" + location.toString(), Toast.LENGTH_SHORT).show(); 
        initCamera(location); 
       } else 
        Toast.makeText(getActivity(), "location is null", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

Antwort

2

Da die Lage asynchron erworben, sollten Sie einen Rückruf verwenden, um die Position zurückkehren, sonst werden Ihr Rückgabewert wird immer null sein, weil die googleApiClient ist noch nicht fertig.

eine Schnittstelle wie diese es

definieren
public interface LocationCallback { 
    void onComplete(Location location); 
} 

ändern

public Location getLocation(Context context) 

zu

public Location getLocation(Context context, LocationCallback callback) 

und nennen dies wie

LocationUtils.getLocation(context, new LocationCallback() { 
    @Override 
    onComplete(Location location) { 
     // when this is called, you will have a location. 
    } 
});