谷歌玩游戏登录流程

时间:2014-07-02 11:38:04

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

我正在开发一款使用Google Play游戏服务的应用。我目前正在进行主屏幕活动,我希望登录流程的工作方式和flappy bird一样。

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

  1. 按钮没有登录
  2. 如果用户点击排行榜或费率按钮,则会要求您登录。<​​/ li>
  3. 如果您尚未登录,则仍可以离线播放
  4. 我希望我的代码能够实现这一点,但我遇到了一些困难。首先,我的应用程序KEEPS要求我在每次开始活动时登录,它不记得我之前已经登录过。我将非常感谢以下代码的帮助。

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

    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();
            }
        }
    }
    

1 个答案:

答案 0 :(得分:1)

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

我建议您使用BaseGameUtils包,而不是扩展BaseGameActivity,只需使用GameHelper directly。这使得使用Play游戏服务带来了很多痛苦。