1

Ich kann nicht scheinen, GoogleApiClient zu machen, um an meinem Projekt zu arbeiten. Ich verwende LocationServices, OnConnected wird nicht ausgelöst, obwohl ich die client.connect() in der onStart Methode genannt habe. Ich habe stackoverflow gesucht und Lösungen versucht, aber ich kann es immer noch nicht funktionieren. Ich versuchte zu überprüfen, ob ich verbunden war, aber client.isConnected() gibt immer false aus. meine Berechtigungen:Kann keine Verbindung zu GoogleAPIClient LocationServices.API

android.permission.INTERNET android.permission.ACCESS_COARSE_LOCATION android.permission.ACCESS_FINE_LOCATION

bin ich etwas fehlt hier?

edit:

private GoogleApiClient client; 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_my); 
ButterKnife.inject(this); 

buildGoogleApiClient(); 
} 

protected synchronized void buildGoogleApiClient() { 
client = new GoogleApiClient.Builder(this) 
.addConnectionCallbacks(this) 
.addOnConnectionFailedListener(this) 
.addApi(LocationServices.API).build(); 
} 

@Override 
public void onStart() { 
// client.connect(); 
super.onStart(); 

// ATTENTION: This was auto-generated to implement the App Indexing API. 
// See https://g.co/AppIndexing/AndroidStudio for more information. 
client.connect(); 
Log.e("Connected?", String.valueOf(client.isConnected())); 

} 

public void onConnectionFailed(ConnectionResult connectionResult) { 
Log.e("Failed?", connectionResult.getErrorMessage()); 
} 


@Override 
public void onConnected(Bundle bundle) { 
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
return; 
} 
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
client); 
longt = mLastLocation.getLongitude(); 
latt = mLastLocation.getLatitude(); 
if (mLastLocation != null) { 
// mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude())); 
// mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude())); 
} 

} 

Antwort

1

Try this !!

Zuerst: buildGoogleApiClient() entfernen; in Oncreate();

Dann:

public static final int REQUEST_CODE_RESOLUTION = 1; 

@Override 
protected void onResume() { 
    super.onResume(); 
    buildGoogleClient(); 
} 

private void buildGoogleClient(){ 
if(client != null){ 
client = new GoogleApiClient.Builder(this) 
.addApi(LocationServices.API) 
.addConnectionCallbacks(this) 
.addOnConnectionFailedListener(this).build(); 
} 
client.connect(); 
} 

    @Override 
public void onConnectionFailed(ConnectionResult result) { 
    Log.i(TAG, "GoogleApiClient connection failed: " + result.toString()); 
    if (!result.hasResolution()) { 
     // show the localized error dialog. 
     GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show(); 
     return; 
    } 
    try { 
     result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION); 
    } catch (SendIntentException e) { 
     Log.e(TAG, "Exception while starting resolution activity", e); 
    } 
} 

    @Override 
protected void onActivityResult(int requestCode, int resultCode, 
     Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) { 
     client.connect(); 

    // Enter code get Latude and Longtude 

    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(client); 
longt = mLastLocation.getLongitude(); 
latt = mLastLocation.getLatitude(); 
if (mLastLocation != null) { 
    mLatitudeText.setText(mLastLocation.getLatitude()+""); 
    mLongitudeText.setText(mLastLocation.getLongitude()+""); 
} 
    } 
} 
+0

es funktionierte, aber ich habe einen anderen Fehler. Ich bin mir nicht sicher, ob das immer noch zusammenhängt. "GoogleApiClient ist nicht für die Verwendung der für diesen Anruf erforderlichen API konfiguriert." – Redan

+0

Sie können das Lernprogramm zum Google Client-Standort unter http://www.androidhive.info/2015/02/android-location-api-using-google-play einsehen -Dienstleistungen/ –