Google Play服务无法连接

时间:2016-07-30 18:31:39

标签: android google-play google-play-services google-play-developer-api

我正在尝试将我的应用程序连接到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注册吗?

2 个答案:

答案 0 :(得分:2)

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

  

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

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

答案 1 :(得分:0)

我在2小时内解决了这个问题,但我忘了回答。 是的,我尝试了提供的解决方案并且有效。我通过引用此线程创建了SHA-1密钥:How can I find and run the keytool,我还在Google Developer Console中使用了与我的项目相同的软件包名称,并使用与我注册的相同的Google帐户连接到Google Play服务Developer Console。工作正常;)