I have tried to follow to Google documentation for Google Play Services but that's impossible, because Google documentation is the worst ever.
Anyway, I manged to setup project and add Google Play Services in my app, but I don't know how to keep connected state of player when user changes activity.
I have MainActivity, where extend BaseGameActivity, and then user click on button Play, I open GameActivity, where I play game, and when I finish game I want to submit score to Google Play Services Leaderboard, but I can't because it seems that I am not connected.
I extend MyActivity with BaseGameActivity
public class MainActivity extends BaseGameActivity
Then I on GameActivity extend this:
public GameActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener
And then on onCreate use this:
// Create the Google Api Client with access to Plus and Games
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.addOnConnectionFailedListener(this)
.addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN)
.addApi(Games.API).addScope(Games.SCOPE_GAMES)
// add other APIs and scopes here as needed
.build();
And then when I finish game I try to submit score, but it seems that I am not connected.
if(mGoogleApiClient.isConnected()){
Games.Achievements.unlock(mGoogleApiClient,
getString(R.string.app_name));
Games.Leaderboards.submitScore(mGoogleApiClient,
getString(R.string.number_guesses_leaderboard),
clickCounter);
}
According to the docs, you need to call mGoogleApiClient.connect() aswell. They recommend using a button for sign in, but you can also do it at onStart():
#Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
If all goes well you'll receive the callback onConnected(Bundle connectionHint) which you are already implementing.
Play Game Services are really made with Fragments in mind. It is almost mandatory to use Fragments instead of Activities, and to have a single Activity in your app...
Related
I’d like to use the Google Play Billing Library for in-app billing in my Android app. When the app launches, I’d like to initialize the BillingClient, start the connection, and retrieve some product data. The actual purchase would happen from a different activity (not the launch activity).
Should I make some kind of singleton billing manager that contains the BillingClient, so I can start it when the app launches and still use it in the other activity? Or is there a better approach?
There is an example provided by Google. In this example, they use additional BillingManager class which can be accessed from different places of your application but it isn't a singleton because it's not only about BillingClient you also have to have PurchasesUpdatedListener which is receiving updates from the BillingManager.
If you want to make BillingClient a singleton you can use BillingManager from the sample app and manage the list of BillingUpdatesListener by yourself.
You can try following approach:
mBillingManager = new BillingManager(this, getUpdateListener());
mBillingManager.startServiceConnection(new Runnable() {
#Override
public void run() {
// Do AnyThing Here
}
});
I try to implement Game Play Service Achievement in my game, but when achievement is unlocked, it does not pops up (show that achievement is unlocked) but when I open list of all achievements it shows that is unlocked. So my problem is, how to pops up achievement when it's unlocked ?
MainActivity.class
public static GoogleApiClient mGoogleApiClient;
private void callGooglePlay(){
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Games.API).addScope(Games.SCOPE_GAMES)
.build();
}
PlayActivity.class
Games.Achievements.unlock(MainMenu.mGoogleApiClient, getResources().getString(R.string.e));
You need to call the getAchievementsIntent() to create the default achievements UI, and then call the startActivityForResult
startActivityForResult(Games.Achievements.getAchievementsIntent(mGoogleApiClient),REQUEST_ACHIEVEMENTS);
More information about displaying achievements can be found on the documentation.
If you need a notification that's specific to the achievement being unlocked, you can try to use [setViewForPopups](https://developers.google.com/android/reference/com/google/android/gms/games/Games.html#setViewForPopups(com.google.android.gms.common.api.GoogleApiClient, android.view.View)) as suggested in this question.
I am developing android app. In my app i provided login with google functionality. If i use following code in same activity then it works well for logout from google.-
if (googleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(googleApiClient);
googleApiClient.disconnect();
googleApiClient.connect();
}
Question -
How to manage google user session using SharedPreferences?
or How to initialize googleApiClient object in SessionManager class?
Thanks.
I have published an Android app using the Google Play Game service and have been informed of an alert that says it has "Detected incorrect implementation". Investigating what this means I found that...
This game has implemented invitations but doesn't allow users to join a match from an invitation. This approach is discouraged because it could result in a poor user experience.
Since I based this game on the sample project (TypeANumber) I am unsure what I need to do to comply with this alert. What should I be looking for in the code that would cause this alert, since I have invited and played against my friends quite happily and not experienced such an issue.
I have encountered (and now cleared) this alert. I suggest the following:
If you have implemented the OnInvitationReceivedListener i.e.
class myClass extends Fragment implements OnInvitationReceivedListener, ...
and do not want to handle this yourself, then simply remove OnInvitationReceivedListener from your
implements clause. This is what I did, and it cleared the alert.
Alternatively, if you do want to handle the invitation, then as well as having the OnInvitationReceivedListener in the implements clause you also need to register the invitation listener at run time i.e.
Games.Invitations.registerInvitationListener(getApiClient(), this)
and provide the listener code i.e.
public void onInvitationReceived(Invitation invitation) {
...
}
I followed the below link to implement a "sign out" button in my android app, which uses a Google API client. However, upon connecting the google api again, the user is not presented with an account picker. It looks like the value of her/his original choice is somehow still cached perhaps. I've been trying to figure this out for a few hours.
Any and all ideas very welcome. Thank you.
https://developers.google.com/+/mobile/android/sign-in
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
}
I've had many problems using clearDefaultAccount and trying to reconnect as well. Finally I've decided to separate the account selection process by using the AccountPicker class (which, by the way, doesn't require global permissions in manifest).
So, when the user wants to connect, always show the AccountPicker and then use the selected account to build your GoogleApiClient (see .setAccountName in GoogleApiClient.Builder).
Everything works smoothly now.
This works for me - use revoke to remove all data in the google client:
public void logout()
{
if (mPlusClient.isConnected())
{
Plus.AccountApi.clearDefaultAccount(mPlusClient);
Plus.AccountApi.revokeAccessAndDisconnect(mPlusClient);
}
}
Afterwards, if you try to login again, you'll be presented an account selector again
You are not being presented with an account picker because you didn't call
mGoogleApiClient.connect() after reconnecting.