谷歌播放服务,结果代码0 onactivityresult

时间:2017-04-06 13:17:02

标签: android-studio oauth google-play-services leaderboard

我一直试图连接谷歌播放服务而没有任何成功。我一直在检查所有密钥和ID都是正确的。

但我的API管理器上没有任何流量。 究竟是什么问题?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.d(TAG, getSHA1CertFingerprint(this));
    Log.d(TAG, getAppIdFromResource(this));

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
            .build();

    textUpdate();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d(TAG, "requestcode " + requestCode + " resultcode " + resultCode);
    if (requestCode == RC_SIGN_IN) {
        mSignInClicked = false;
        mResolvingConnectionFailure = false;
        if (resultCode == RESULT_OK) {
            mGoogleApiClient.connect();
        } else {
            BaseGameUtils.showActivityResultError(this, requestCode, resultCode, R.string.signin_other_error);
        }
    }
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
    startActivity(new Intent(Highscore.this,StartScreen.class));
}



public void btnGoogleHigh (View view){
    if (isSignedIn()) {
        startActivityForResult(Games.Leaderboards.getAllLeaderboardsIntent(mGoogleApiClient),
                RC_UNUSED);
    } else {
        BaseGameUtils.makeSimpleDialog(this, getString(R.string.leaderboards_not_available)).show();
    }
}

public void btnLogin (View view){
    mSignInClicked = true;
    mGoogleApiClient.connect();
}
public void btnLogOut (View view){
    mSignInClicked = false;
    Games.signOut(mGoogleApiClient);
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}


@Override
public void onConnected(@Nullable Bundle bundle) {
    Log.d(TAG, "onConnected(): connected to Google APIs");
}

@Override
public void onConnectionSuspended(int i) {
    Log.d(TAG, "onConnectionSuspended(): attempting to connect");
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.d(TAG, "onConnectionFailed(): attempting to resolve");
    if (mResolvingConnectionFailure) {
        Log.d(TAG, "onConnectionFailed(): already resolving");
        return;
    }

    if (mSignInClicked || mAutoStartSignInFlow) {
        mAutoStartSignInFlow = false;
        mSignInClicked = false;
        mResolvingConnectionFailure = true;
        if (!BaseGameUtils.resolveConnectionFailure(this, mGoogleApiClient, connectionResult,
                RC_SIGN_IN, getString(R.string.signin_other_error))) {
            mResolvingConnectionFailure = false;
        }
    }
}

private boolean isSignedIn() {
    return (mGoogleApiClient != null && mGoogleApiClient.isConnected());
}

@Override
protected void onStart() {
    super.onStart();
    Log.d(TAG, "onStart(): connecting");
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    Log.d(TAG, "onStop(): disconnecting");
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

static String getAppIdFromResource(Context ctx) {
    try {
        Resources res = ctx.getResources();
        String pkgName = ctx.getPackageName();
        int res_id = res.getIdentifier("app_id", "string", pkgName);
        return res.getString(res_id);
    } catch (Exception ex) {
        ex.printStackTrace();
        return "??? (failed to retrieve APP ID)";
    }
}

static String getSHA1CertFingerprint(Context ctx) {
    try {
        Signature[] sigs = ctx.getPackageManager().getPackageInfo(
                ctx.getPackageName(), PackageManager.GET_SIGNATURES).signatures;
        if (sigs.length == 0) {
            return "ERROR: NO SIGNATURE.";
        } else if (sigs.length > 1) {
            return "ERROR: MULTIPLE SIGNATURES";
        }
        byte[] digest = MessageDigest.getInstance("SHA1").digest(sigs[0].toByteArray());
        StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < digest.length; ++i) {
            if (i > 0) {
                hexString.append(":");
            }
            byteToString(hexString, digest[i]);
        }
        return hexString.toString();

    } catch (PackageManager.NameNotFoundException ex) {
        ex.printStackTrace();
        return "(ERROR: package not found)";
    } catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
        return "(ERROR: SHA1 algorithm not found)";
    }
}
static void byteToString(StringBuilder sb, byte b) {
    int unsigned_byte = b < 0 ? b + 256 : b;
    int hi = unsigned_byte / 16;
    int lo = unsigned_byte % 16;
    sb.append("0123456789ABCDEF".substring(hi, hi + 1));
    sb.append("0123456789ABCDEF".substring(lo, lo + 1));
}

1 个答案:

答案 0 :(得分:0)

尝试查看有关&#34; Troubleshooting Issues in Your Android Game&#34;的文件。和&#34; Debugging Your Android Games&#34;。此文档将帮助您调试和解决游戏中遇到的问题。

  

如果您无法在玩家中登录玩家,请首先确保您已按照说明创建客户ID并配置游戏服务。如果您仍然遇到登录错误,请检查以下项目以确保您的游戏设置正确。

     
      
  • 检查元数据标记
  •   
  • 检查您的包裹名称
  •   
  • 检查证书指纹
  •   
  • 检查是否已启用测试帐户
  •   

出于代码实现目的,请通过Google查看samples。这将显示基本登录,多人游戏,云节省等。

希望这有帮助。