android - Google Play 游戏登录流程

标签 android google-play-services google-play-games

我正在开发一款使用 Google Play 游戏服务的应用。我目前正在处理主屏幕 Activity ,我希望登录流程能够像 flappy bird 一样工作。

在 flappy bird 中(这只是一个例子,我相信很多其他应用程序也是这样工作的)...

  1. 没有登录按钮
  2. 如果用户点击排行榜或评分按钮,它会要求您登录。<​​/li>
  3. 如果您没有登录,您仍然可以离线玩

我希望我的代码能够实现它,但我遇到了一些困难。首先,每次我开始 Activity 时,我的应用程序一直要求我登录,它不记得我之前已经登录过。我希望能为我的以下代码提供一些帮助。

到目前为止,这是我的代码:

public class HomeScreenActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener {

    private Button playButton, leaderboardButton;
    private Context appContext;
    private static final int GAME_INTENT = 1000;
    private GoogleApiClient mGoogleApiClient;
    // Request code to use when launching the resolution activity
    private static final int REQUEST_RESOLVE_ERROR = 1001;
    private static final int REQUEST_LEADERBOARD = 1002;
    // Unique tag for the error dialog fragment
    private static final String DIALOG_ERROR = "dialog_error";
    // Bool to track whether the app is already resolving an error
    private boolean mResolvingError = false;
    private static final String STATE_RESOLVING_ERROR = "resolving_error";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_screen_layout);

        mResolvingError = savedInstanceState != null
                && savedInstanceState.getBoolean(STATE_RESOLVING_ERROR, false);

        appContext = getApplicationContext();

        findViews();
        attachListeners();

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

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!mResolvingError) {
            mGoogleApiClient.connect();
        }
    }

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

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(STATE_RESOLVING_ERROR, mResolvingError);
    }

    private void findViews() {
        playButton = (Button) findViewById(R.id.play_button);
        leaderboardButton = (Button) findViewById(R.id.leaderboard_button);
    }

    private void attachListeners() {
        playButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(appContext, InGameActivity.class);
                startActivityForResult(i, GAME_INTENT);
            }

        });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case GAME_INTENT:
            // upload data to leaderboards
            break;
        case REQUEST_RESOLVE_ERROR:
            mResolvingError = false;
            if (resultCode == RESULT_OK) {
                // Make sure the app is not already connected or attempting to
                // connect
                if (!mGoogleApiClient.isConnecting()
                        && !mGoogleApiClient.isConnected()) {
                    mGoogleApiClient.connect();
                }
            }
            break;
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        if (mResolvingError) {
            // Already attempting to resolve an error.
            return;
        } else if (result.hasResolution()) {
            try {
                mResolvingError = true;
                result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
            } catch (SendIntentException e) {
                // There was an error with the resolution intent. Try again.
                mGoogleApiClient.connect();
            }
        } else {
            // Show dialog using GooglePlayServicesUtil.getErrorDialog()
            showErrorDialog(result.getErrorCode());
            mResolvingError = true;
        }
    }

    @Override
    public void onConnected(Bundle arg0) {
        Log.d("LOG", "+++++ onConnected +++++");
        leaderboardButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivityForResult(Games.Leaderboards.getLeaderboardIntent(
                        mGoogleApiClient, getString(R.string.leaderboard_highest_score)), REQUEST_LEADERBOARD);
            }

        });
    }

    @Override
    public void onConnectionSuspended(int arg0) {
        Log.d("LOG", "+++++ onConnectionSuspended +++++");
        leaderboardButton.setOnClickListener(null);
    }

     /* Creates a dialog for an error message */
    private void showErrorDialog(int errorCode) {
        // Create a fragment for the error dialog
        ErrorDialogFragment dialogFragment = new ErrorDialogFragment();
        // Pass the error that should be displayed
        Bundle args = new Bundle();
        args.putInt(DIALOG_ERROR, errorCode);
        dialogFragment.setArguments(args);
        dialogFragment.show(getFragmentManager(), "errordialog");
    }

    /* Called from ErrorDialogFragment when the dialog is dismissed. */
    public void onDialogDismissed() {
        mResolvingError = false;
    }

    /* A fragment to display an error dialog */
    public static class ErrorDialogFragment extends DialogFragment {
        public ErrorDialogFragment() { }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Get the error code and retrieve the appropriate dialog
            int errorCode = this.getArguments().getInt(DIALOG_ERROR);
            return GooglePlayServicesUtil.getErrorDialog(errorCode,
                    this.getActivity(), REQUEST_RESOLVE_ERROR);
        }

        @Override
        public void onDismiss(DialogInterface dialog) {
            ((HomeScreenActivity)getActivity()).onDialogDismissed();
        }
    }
}

最佳答案

如果您不使用 BaseGameUtils,您将需要自己维护和检查状态执行登录和其他 Play 游戏相关任务。这非常困惑,但您可以查看此包中的 GameHelper 类,了解 Google 是如何做到的。

我建议您使用 BaseGameUtils 包,而不是扩展 BaseGameActivity 只需使用 GameHelper directly .这大大减轻了使用 Play 游戏服务的痛苦。

关于android - Google Play 游戏登录流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24530140/

相关文章:

java - 来自android中月份数字的月份名称

android - Telegram API release.keystore 错误

unity-game-engine - Google Play 游戏服务登录时应用程序崩溃

android - 为非游戏应用程序使用 Saved Game Service

android - 加载图像并单击上一个按钮不会在 android 中加载图像

android - 来自库项目的 JAR 在应用程序项目中不可见

Android gms ImageManager 从不加载图像

android - 错误 :(50, 28) 找不到与给定名称匹配的资源(在 'value' 处,值为 '@string/google_maps_key')

android - GoogleSignInClient 返回 8(内部错误)

安卓回合制多人自定义邀请画面