【问题标题】:Google Play Services fails to connectGoogle Play 服务无法连接
【发布时间】:2016-07-30 18:31:38
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 哦,我以为我的应用程序的 id 是应用程序名称旁边的数字。我将尝试生成正确的 OAuth2 令牌并重试,谢谢,
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2019-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多