实施 Google Play 游戏服务 - 如何让用户登录

Posted

技术标签:

【中文标题】实施 Google Play 游戏服务 - 如何让用户登录【英文标题】:Implementing Google Play Game Services - How to let user sign in 【发布时间】:2016-12-20 17:59:41 【问题描述】:

这是我第一次使用 google API,因为我对 android 编程还很陌生。我一直在尝试在我的应用程序中实现 google play 游戏服务和显示排行榜(这是您可能已经猜到的游戏),并且在遵循 google 的文档时遇到了一些困难。

我设置了一个按钮,并希望在用户点击它时让用户登录,并让用户保持登录状态,直到他们随后退出。我已经按照文档对此进行了编程:

public class MainActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener 

  Button bLeaderboard;

  private GoogleApiClient mGoogleApiClient;

  private static int RC_SIGN_IN = 9001;

  private boolean mResolvingConnectionFailure = false;
  private boolean mAutoStartSignInflow = true;
  private boolean mSignInClicked = false;


@Override
@SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) 

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bLeaderboard = (Button) findViewById(R.id.bLeaderboard);
    bLeaderboard.setOnClickListener(this);

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


@Override
public void onClick(View v) 
    switch (v.getId()) 

        case R.id.bLeaderboard:

            mSignInClicked = true;
            mGoogleApiClient.connect();



问题是,每当我点击排行榜按钮时,应用程序就会崩溃,并且我会收到以下错误消息:

AndroidRuntime: 致命异常: main 进程:com.example.game,PID:26284 java.lang.IllegalStateException:发生了致命的开发人员错误。检查日志以获取更多信息。 在 com.google.android.gms.common.internal.zzd$zza.zzc(未知来源) 在 com.google.android.gms.common.internal.zzd$zza.zzv(未知来源) 在 com.google.android.gms.common.internal.zzd$zze.zzasf(未知来源) 在 com.google.android.gms.common.internal.zzd$zzd.handleMessage(未知来源) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:148) 在 android.app.ActivityThread.main(ActivityThread.java:5417) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我也一直无法理解如何处理这三个实现的方法:

@Override
public void onConnected(@Nullable Bundle bundle) 


@Override
public void onConnectionSuspended(int i) 


@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) 


非常感谢任何帮助,因为除了 google 的文档之外,我找不到任何关于此的教程,正如我之前所说,我很难理解和遵循。

【问题讨论】:

【参考方案1】:

这对我很有效

 public class MainActivity extends ActionBarActivity implements  GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener 

// Client used to interact with Google APIs
private GoogleApiClient mGoogleApiClient;
private static int RC_SIGN_IN = 9001;

private boolean mResolvingConnectionFailure = false;
private boolean mAutoStartSignInflow = true;
private boolean mSignInClicked = false;
GameHelper gameHelper;



@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);


    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
            .build();
    gameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
    gameHelper.enableDebugLog(true);
    gameHelper.setup(new GameHelper.GameHelperListener() 
        @Override
        public void onSignInFailed() 
        Toast.makeText(getApplicationContext(),"Please Check Your Internet Connection!",Toast.LENGTH_LONG).show();
        

        @Override
        public void onSignInSucceeded() 
            Toast.makeText(getApplicationContext(),"Signed in Succeed",Toast.LENGTH_LONG).show();
        
    );



    setContentView(view);

    //for load all Score card



    scorecard=(Button)findViewById(R.id.score_card);
    scorecard.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 


            startActivityForResult(Games.Leaderboards.getAllLeaderboardsIntent(mGoogleApiClient), 1);

        
    );






private boolean isScoreResultValid(final Leaderboards.LoadPlayerScoreResult scoreResult) 
    return scoreResult != null && GamesStatusCodes.STATUS_OK == scoreResult.getStatus().getStatusCode() && scoreResult.getScore() != null;




@Override
protected void onStart() 
    super.onStart();
    mGoogleApiClient.connect();


@Override
protected void onStop() 
    super.onStop();
    mGoogleApiClient.disconnect();


@Override
public void onConnected(Bundle connectionHint) 
    // The player is signed in. Hide the sign-in button and allow the
    // player to proceed.
    // now submit your high_score
    Games.Leaderboards.submitScore(mGoogleApiClient,getResources().getString(R.string.leaderboard_easy),high_score);



@Override
public void onConnectionFailed(ConnectionResult connectionResult) 
    if (mResolvingConnectionFailure) 
        // already resolving
        return;
    

    // if the sign-in button was clicked or if auto sign-in is enabled,
    // launch the sign-in flow
    if (mSignInClicked || mAutoStartSignInflow) 
        mAutoStartSignInflow = false;
        mSignInClicked = false;
        mResolvingConnectionFailure = true;

        // Attempt to resolve the connection failure using BaseGameUtils.
        // The R.string.signin_other_error value should reference a generic
        // error string in your strings.xml file, such as "There was
        // an issue with sign-in, please try again later."
        /*if (!BaseGameUtils.resolveConnectionFailure(this,
                mGoogleApiClient, connectionResult,
                RC_SIGN_IN, "Sign in error")) 
            mResolvingConnectionFailure = false;
        */
    

    // Put code here to display the sign-in button


@Override
public void onConnectionSuspended(int i) 
    // Attempt to reconnect
    mGoogleApiClient.connect();




protected void onActivityResult(int requestCode, int resultCode,
                                Intent intent) 
    if (requestCode == RC_SIGN_IN) 
        mSignInClicked = false;
        mResolvingConnectionFailure = false;
        if (resultCode == RESULT_OK) 
            mGoogleApiClient.connect();
         else 
            // Bring up an error dialog to alert the user that sign-in
            // failed. The R.string.signin_failure should reference an error
            // string in your strings.xml file that tells the user they
            // could not be signed in, such as "Unable to sign in."
            BaseGameUtils.showActivityResultError(this,
                    requestCode, resultCode, R.string.signin_failure);
        
    

// Call when the sign-in button is clicked
private void signInClicked() 
    mSignInClicked = true;
    mGoogleApiClient.connect();


// Call when the sign-out button is clicked
private void signOutclicked() 
    mSignInClicked = false;
    Games.signOut(mGoogleApiClient);


 

【讨论】:

记住还要在google play game service console中声明测试者的email-id..

以上是关于实施 Google Play 游戏服务 - 如何让用户登录的主要内容,如果未能解决你的问题,请参考以下文章

实施 Google Play 游戏时出错

libGDX Google Play 游戏服务 - Android

Google Play 游戏服务

在 Google Play 游戏服务中跟踪输赢

Android libgdx:使用 Google Play 服务时出错

如何在使用 Google Play 游戏服务的实时多人游戏中使用机器人玩家?