0

我正在尝试将我的应用程序连接到 Google Play 服务,但我可以检查,它似乎没有正确连接。当我单击检查是否已连接的按钮时,它返回未连接的我。但是,该应用程序可以访问 Google Play 服务并让我检查要使用的电子邮件帐户。

这是代码:

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;
//GoogleApiClient

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    client = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
            .build();
    Button button  = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Games.signOut(client);
            TextView textView = (TextView) findViewById(R.id.helloworld);
            //client.connect();
            if(client.isConnected()){
                textView.setText("Buttonconnected");

            }else{

                textView.setText("Buttonnotconnected");
            }
        }
    });


    if (client == null) {

    }
}

@Override
public void onStart() {
    super.onStart();
    //Log.i("app","Lapp");
    if (client.isConnected()) {
        //TextView t = (TextView) findViewById(R.id.helloworld);
        //t.setText("Connected");
    }
   /* GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
    int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(this);
    if (resultCode == ConnectionResult.SUCCESS) {
        Toast.makeText(this, "Google Play Works!!", Toast.LENGTH_LONG).show();
    } else{
        Toast.makeText(this, "Google Play Services required!!", Toast.LENGTH_LONG).show();
    }*/
    if (client != null) {
        client.connect();
    }

    if (client.isConnected()) {
        TextView t = (TextView) findViewById(R.id.helloworld);
        t.setText("Connected");
    }

}

@Override
public void onStop() {
    super.onStop();

}

public void onConnectionSuspended(int cause) {
    // We are not connected anymore!
    Log.i("app", "connectionsuspended");
}

public void onConnected(Bundle connectionHint) {

}

@Override
public void onConnectionFailed(ConnectionResult result) {
    // We tried to connect but failed!
    TextView t;
  //        t = (TextView) findViewById(R.id.helloworld);
          //      t.setText("Failed");


    try {
        //result.startResolutionForResult(this, 49404);
        result.startResolutionForResult(this, 1001);
        Log.i("app", "show");
    } catch (Exception e) {
        Log.i("app", e.toString());
    }


    client.connect();
    client.hashCode();
    Toast.makeText(this, "Toasting", Toast.LENGTH_LONG).show();
    if (client.isConnected()) {
        Log.i("app", "worked");
        t = (TextView) findViewById(R.id.helloworld);
        t.setText("FailedConnected");
    }
    if (client.isConnecting()) {
        t = (TextView) findViewById(R.id.helloworld);
        t.setText("Connecting");
        Log.i("app", "Failedtryingtoconnect");

    }


}

}

这是我的 build.gradle:

……

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.google.android.gms:play-services:9.0.2'
 }

这是我的 Manifest.xml

……

 <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data android:name="com.google.android.gms.games.APP_ID"
        android:value="@string/app_id" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />

...

我错过了什么吗?我正在使用与我在 Google Developers Console 中注册的帐户相同的帐户进行此测试。我错过了 OAuth2 注册吗?

4

2 回答 2

2

是的,我认为您缺少用于身份验证的 OAuth2 ID。从文档:

您的游戏必须具有 OAuth 2.0 客户端 ID,才能通过身份验证和授权调用 Google Play 游戏服务。要设置客户端 ID 与您的游戏之间的关联,请使用 Google Play 开发者控制台生成客户端 ID 并将其链接到您的游戏。

https://developers.google.com/games/services/console/enabling

于 2016-07-30T18:49:59.340 回答
0

我在 2 小时内解决了这个问题,但我忘了回答。是的,我尝试了提供的解决方案并且它有效。我通过引用这个线程创建了 SHA-1 密钥:如何找到并运行 keytool,我还在 Google Developer 控制台中使用了与我的项目相同的包名称,并使用相同的 google 帐户连接到 Google Play Services我在开发者控制台注册。所以工作正常;)

于 2016-08-02T16:24:42.633 回答