【问题标题】:GoogleApiClient won´t connectGoogleApiClient 无法连接
【发布时间】:2015-01-22 23:11:56
【问题描述】:

我想使用 FusedLocationProviderApi 来获取最后一个已知位置。我按照谷歌教程所说的那样做,但似乎 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

}

【问题讨论】:

    标签: android google-api-client fusedlocationproviderapi


    【解决方案1】:

    你错过了

    mGoogleApiClient.connect();

    【讨论】:

      猜你喜欢
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-17
      • 1970-01-01
      相关资源
      最近更新 更多