(Фон: все, что я хочу - это уникальный и постоянный идентификатор Google Play для пользователя (даже после удалений или на разных устройствах). Это единственная причина, по которой я это делаю .)Службы Google Play: невозможно подключиться к серверу
Я использую Кордову. Это моя основная деятельность.
Проблема: функция onConnected
никогда не запускается. Тем не менее, я могу подписаться. (Я вижу значок в окне, знак в круге и все остальное), но он просто не запускается.
ПРИМЕЧАНИЕ: onConnectionFailed запускается один раз с кодом состояния SIGN_IN_REQUIRED.
package com.myapp;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import com.google.android.gms.common.ConnectionResult;
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.common.GooglePlayServicesUtil;
import com.google.android.gms.games.Players;
import com.google.android.gms.games.Games;
import android.os.Bundle;
import org.apache.cordova.*;
import android.util.Log;
public class MyApp extends CordovaActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static final String LOGTAG = "GooglePlayServices";
// Client used to interact with Google APIs.
private GoogleApiClient mGoogleApiClient;
private CordovaActivity activity;
boolean mResolvingError;
@Override public void onCreate (Bundle savedInstanceState) {
activity = this;
super.onCreate (savedInstanceState);
super.init();
mGoogleApiClient = new GoogleApiClient.Builder (this)
.addConnectionCallbacks (this)
.addOnConnectionFailedListener (this)
.addApi (Games.API)
.addScope(Games.SCOPE_GAMES)
.build();
mGoogleApiClient.connect();
super.loadUrl(Config.getStartUrl());
}
@Override public void onConnectionFailed (ConnectionResult result) {
if (mResolvingError) return;
if (!result.hasResolution()) {mResolvingError = true; return;}
Log.d (LOGTAG, result.toString());
try {
mResolvingError = true;
result.startResolutionForResult (this, result.getErrorCode());
} catch (SendIntentException e) {
// There was an error with the resolution intent. Try again.
mGoogleApiClient.connect();
}
}
@Override public void onConnected (Bundle connectionHint) {
// This never runs... this is the most critical part. I need the player ID!
String playerId = Games.Players.getCurrentPlayerId (mGoogleApiClient);
Log.w (LOGTAG, playerId);
}
// I saw this one with an @Override in others' code, but it won't compile if I add that.
public void onDisconnected() {}
protected void onStart() {super.onStart(); mGoogleApiClient.connect();}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) mGoogleApiClient.disconnect();
}
protected void onActivityResult (int requestCode, int responseCode, Intent intent) {
if (!mGoogleApiClient.isConnecting()) mGoogleApiClient.connect();
}
public void onConnectionSuspended (int cause) {mGoogleApiClient.connect();}
}