问题
I want to connect my game with google play service. i have read documentation on android developer and try to following type-a-number sample and still can't load leaderboard.
i have import baseGameUtils, but i use andengine so i didn't use extends BaseGameActivity from google.
what i have until now:
- GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) return success
- startActivityForResult(pickAccountIntent, REQUEST_CODE_PICK_ACCOUNT); is working well and i got my account name from onActivityResult(..);
- i already put this on my manifest.
<meta-data android:name="com.google.android.gms.games.APP_ID"
android:value="@string/app_id" />
my questions are
1. can i use google play service without extends BaseGameActivity?
2. if i use gameHelper.beginUserInitiatedSignIn(); after i got my account name, i got this on log cat. (what this connected mean? because i still got error on next question)
08-25 00:09:01.890: D/BaseGameActivity(11222): isGooglePlayServicesAvailable returned 0
08-25 00:09:01.890: D/BaseGameActivity(11222): beginUserInitiatedSignIn: starting new sign-in flow.
08-25 00:09:01.890: D/BaseGameActivity(11222): All clients now connected. Sign-in successful.
08-25 00:09:01.890: D/BaseGameActivity(11222): All requested clients connected. Sign-in succeeded!
3 . how do i use connect()? i have read and tried about gameClient and GameClientBuilder but i have no idea how to use that. when i tried run this code.
startActivityForResult(gameHelper.getGamesClient().getAllLeaderboardsIntent(), RC_UNUSED);
i got this log.
08-25 00:09:05.660: E/AndroidRuntime(11222): java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
4 . to use leaderboard i know i must use code from google play store such as CgkIx****AIQAA. but i didn't found where i must put this code to load leaderboard.
sorry for long question, but i think if there is a sample that only for connect and either access achievement or leaderboard it will answer all my question. please don't tell me to see type-a-number sample, i did that and i need another sample code.
update, my snipped code
public class MainMenu extends Activity
implements OnClickListener, GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, GameHelperListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
gameHelper = new GameHelper(this);
}
@Override
public void onClick(View v) {
if(v.equals(loadData)) {
if(gameHelper.isSignedIn()) {
gameHelper.setup(this, GameHelper.CLIENT_GAMES, Scopes.GAMES);
startActivityForResult(gameHelper.getGamesClient().getAllLeaderboardsIntent(), RC_UNUSED);
}
}
else if(v.equals(loginButton)) {
Intent googlePicker = AccountPicker.newChooseAccountIntent(null,null,new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE},true,null,null,null,null) ;
startActivityForResult(googlePicker, REQUEST_CODE_PICK_ACCOUNT);
}
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
if(requestCode==REQUEST_CODE_RECOVER_PLAY_SERVICES) {
if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Google Play Services must be installed.", Toast.LENGTH_SHORT).show();
finish();
}
return;
}
else if(requestCode==REQUEST_CODE_PICK_ACCOUNT) {
if (resultCode == RESULT_OK) {
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
gameHelper.beginUserInitiatedSignIn();
}
else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "This application requires a Google account.", Toast.LENGTH_SHORT).show();
finish();
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
// this 2 methods not called, is this also because my code is wrong?
@Override
public void onSignInFailed() {
Log.d("rush", "on sign in failed");
}
@Override
public void onSignInSucceeded() {
Log.d("rush", "on sign in succeed");
}
}
回答1:
Yes. Take a look at the
BaseGameActivity
source and see that it largely just wrapsGameHelper
. You can implement the calls toGameHelper
yourself - in fact, you can probably copy some code directly fromBaseGameActivity
. I'm a bit confused, because it appears that your code is already usingGameHelper
. It looks like you are mixingGameHelper
calls withBaseGameActivity
calls. You cannot do this, and it will result in... errors like you are getting.The LogCat you see means that all of your clients are connected. The default call to
GameHelper.setup()
just requests the Games client. If you aren't usingBaseGameActivity
and want different clients, do:gameHelper = new GameHelper(this); gameHelper.setup(this, GameHelper.CLIENT_GAMES | GameHelper.CLIENT_PLUS);
beginUserInitiatedSignIn()
is an asynchronous method with a callback when it finishes. Are you running it that way?GameHelper.GameHelperListener
is the interface to implement. If you are using gameHelper, make sure to register the callback. See thethis
in the setup call above? That's registering the callback (this
is my main activity).As I said above, it looks like you are mixing
GameHelper
calls withBaseGameActivity
calls. TheGameHelper
that is connected is theBaseGameActivity.mHelper
instance, not anyGameHelper
you might have instantiated. Make sure that if you are usingBaseGameActivity
that you are not usingGameHelper
as well.If you want to display a single leaderboard, use the
GamesClient.getLeaderboardIntent(string, int)
or method to get theIntent
. The string is the code you have (CgkIx****AIQAA).startActivityForResult(gameHelper.getGamesClient().getLeaderboardIntent( leaderboard_id, RC_UNUSED);
Again, make sure you are using the correct
getGamesClient()
method, depending on if you are usingBaseGameActivity
orGameHelper
directly.
回答2:
Here is basic information how to use GameHelper without BaseGameActivity:
https://developers.google.com/games/services/android/init#using_gamehelper_without_basegameactivity
来源:https://stackoverflow.com/questions/18421635/how-to-connect-to-google-play-service-and-load-leaderboard