简体   繁体   English

Google Play服务无法连接

[英]Google Play Services fails to connect

I am trying to connect my app to Google Play Services but, as I can check, it seems not to connect it properly. 我正在尝试将我的应用连接到Google Play服务,但据我检查,似乎无法正确连接它。 When I click the button which checks if it is connected it returns me that is not connected. 当我单击检查是否已连接的按钮时,它将返回未连接的我。 However the app accesses to the Google Play Services and lets me to check which e-mail account to use. 但是,该应用程序可以访问Google Play服务,并可以让我检查要使用的电子邮件帐户。

Here is the code: 这是代码:

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");

    }


}

}

This is my build.gradle: 这是我的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'
 }

and this is my Manifest.xml 这是我的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" />

... ...

Am I missing something? 我想念什么吗? I am using for this testing the same account as I registered in Google Developers Console. 我为此测试所使用的帐户与我在Google Developers Console中注册的帐户相同。 Am I missing OAuth2 signup? 我是否缺少OAuth2注册?

Yes i think you are missing OAuth2 ID for authentication. 是的,我认为您缺少用于身份验证的OAuth2 ID。 From documentation: 从文档:

Your game must have an OAuth 2.0 client ID in order to be authenticated and authorized to call the Google Play games services. 您的游戏必须具有OAuth 2.0客户端ID,以便通过身份验证和授权才能调用Google Play游戏服务。 To set up the association between a client ID and your game, use the Google Play Developer Console to generate the client ID and link it to your game. 要设置客户端ID和您的游戏之间的关联,请使用Google Play开发者控制台生成客户端ID并将其链接到您的游戏。

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

I solved this in 2 hours, but I forgot to answer. 我在2小时内解决了这个问题,但我忘了回答。 Yes I tried the solution provided and it worked. 是的,我尝试了提供的解决方案,并且有效。 I created the SHA-1 key by using by reference this thread: How can I find and run the keytool , I also used in Google Developer console the same package name as my project and I connect to Google Play Services using the same google account as I registered in Developer Console. 我通过参考以下线程来创建SHA-1密钥: 如何查找和运行keytool ,我还在Google Developer Console中使用了与项目相同的程序包名称,并使用与以下相同的google帐户连接到Google Play服务我在开发者控制台中注册。 So works fine ;) 所以工作正常;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM