Answer the question
In order to leave comments, you need to log in
What is the error when connecting to google API?
I am writing an android application using the google game services API.
An error occurs when trying to connect GoogleApiClient.
Here is what is written in the onConnectionFailed handler:
ConnectionResult{statusCode=RESOLUTION_REQUIRED, resolution=PendingIntent{413bd288: [email protected]}, message=null} I
wrote in the manifest:
<meta-data android:name="com.google.android.gms.games.APP_ID"
android:value="@string/google_app_id" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
client = new GoogleApiClient.Builder(this).addApi(Games.API).addScope(Games.SCOPE_GAMES).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
Answer the question
In order to leave comments, you need to log in
You are creating the client too early, this should be done in MainActivity in onCreate
Here is my activation code. Now there is no error, but it just does not turn on. That is, OnConnected is not called
package hakito.pencilrunner.Activities;
import android.content.IntentSender;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.games.Games;
import hakito.pencilrunner.Game.Game;
import hakito.pencilrunner.R;
public class FinishActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
public static final String EXTRA_DISTANCE="distance";
GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_finish);
findViewById(R.id.buttonRestart).setOnClickListener(this);
client = new GoogleApiClient.Builder(this).addApi(Games.API).addScope(Games.SCOPE_GAMES).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.buttonRestart:
Game.get().restart();
finish();
break;
}
}
@Override
protected void onStart() {
super.onStart();
client.connect();
}
@Override
protected void onStop() {
super.onStop();
client.disconnect();
}
@Override
public void onConnected(Bundle bundle) {
int dist = getIntent().getIntExtra(EXTRA_DISTANCE, 0);
Games.Leaderboards.submitScore(client, getString(R.string.leaderboard_best_score), dist);
startActivity(Games.Leaderboards.getLeaderboardIntent(client, getString(R.string.leaderboard_best_score)));
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if(connectionResult.hasResolution())
{
try {
connectionResult.startResolutionForResult(this, 0);
client.connect();
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}else {
GooglePlayServicesUtil.showErrorDialogFragment(connectionResult.getErrorCode(), this, 0);
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question