GoogleApiClient无法连接

时间:2015-01-22 23:11:57

标签: android google-api-client fusedlocationproviderapi

我想使用FusedLocationProviderApi来获取最后的已知位置。我就像Google教程所说的那样,但似乎GoogleApiClient没有连接,至少它不会触发onConnected和onConnectionFailed方法。我希望你能提前帮助我。

import com.example.stoos.data.DataModelOrt;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLoadedCallback;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
.
.
.
public class OrtHinzufugen extends Activity  implements
ConnectionCallbacks, OnConnectionFailedListener{
.
.
.
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ort_auswahlen);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    buildGoogleApiClient();
.
.
.
}
protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(LocationServices.API)
        .build();
}
.
.
.
@Override
public void onConnectionFailed(ConnectionResult arg0) {
    System.out.println("Failed");

}

@Override
public void onConnected(Bundle connectionHint) {
    System.out.println("check1?");
    Location mLastLocation;
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
    if (mLastLocation != null) {
        if(mLastLocation.getAccuracy() > 100){
            Toast.makeText(getApplicationContext(), "Für bessere Ergebnisse GPS anmachen", Toast.LENGTH_LONG).show();
        }
        Intent ServiceIntent = new Intent(getApplicationContext(),
                SendeAnfrageService.class);
        ServiceIntent.putExtra("LATITUDE", mLastLocation.getLatitude());
        ServiceIntent.putExtra("LONGITUDE", mLastLocation.getLongitude());
        startService(ServiceIntent); 
        System.out.println("check2?");
    }else{
        Toast.makeText(getApplicationContext(), "Probleme beim laden der Orte. Versuche es bitte nocheinmal", Toast.LENGTH_LONG).show();
    }

}

@Override
public void onConnectionSuspended(int arg0) {
    // TODO Auto-generated method stub

}

1 个答案:

答案 0 :(得分:7)

你错过了

  

mGoogleApiClient.connect();