Google Play服务无法连接 [英] Google Play Services fails to connect

查看:381
本文介绍了Google Play服务无法连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



以下是代码:

  public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
$ b $ / **
*注意:这是自动生成的以实现App Indexing API。
*有关更多信息,请参阅https://g.co/AppIndexing/AndroidStudio。
* /
私有GoogleApiClient客户端;
// 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(); $ b $ 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(连接);
}

}

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

}

public void onConnectionSuspended(int cause){
//我们不再连接!
Log.i(app,connectionsuspended);
}

public void onConnected(Bundle connectionHint){

}

@Override
public void onConnectionFailed(ConnectionResult result ){
//我们尝试连接但失败!
TextView t;
// t =(TextView)findViewById(R.id.helloworld);
// t.setText(Failed);


尝试{
//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(正在连接);
Log.i(app,Failedtryingtoconnect);

}


}

}

这是我的build.gradle:

....



<$
编译fileTree(dir:'libs',include:['* .jar'])
testCompile'junit:junit:4.12'
编译'com.android.support:appcompat-v7:24.0.0'
compile'c​​om.google.android.gms:play-services:9.0.2'
}


$ b

这是我的Manifest.xml / p>

 <元数据
android:name =com.google.android.gms.version
android:value =@ integer / google_play_services_version/>
< meta-data android:name =com.google.android.gms.game.gifs.APP_ID
android:value =@ string / app_id/>
< uses-permission android:name =android.permission.INTERNET/>
<使用权限android:name =android.permission.GET_ACCOUNTS/>
< uses-permission android:name =android.permission.USE_CREDENTIALS/>

...



我是否缺少什么?我使用的是与我在Google Developers Console中注册的帐户相同的帐户。我是否缺少OAuth2注册?

解决方案

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


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


https://开发人员.google.com / games / services / console / enable


I am trying to connect my app to Google Play Services but, as I can check, it seems not to connect it properly. 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.

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:

....

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

....

 <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. Am I missing OAuth2 signup?

解决方案

Yes i think you are missing OAuth2 ID for authentication. 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. 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.

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

这篇关于Google Play服务无法连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆