GamesClient does not connect. Error: "Call connect () and wait for onConnected () to be called."

I am trying to use GamesClient to use the Google Play Game Services leaderboards. Right now I have it when the importbutton button is importbutton , GamesClient used to send some points. As you can see below, I get the error connect() and wait for onConnectd() to be called.

What am I doing wrong? In some tutorials, I see something like PlusClient . I need this? If necessary, I can provide more code.

There seem to be a lot of questions on StackOverflow about these new Google Play Game services, but there aren't many answers there. It looks like people are still learning - like me. :)

Logcat

 06-12 00:40:40.173: E/AndroidRuntime(1685): java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called. 06-12 00:40:40.173: E/AndroidRuntime(1685): at com.google.android.gms.internal.pn(Unknown Source) 06-12 00:40:40.173: E/AndroidRuntime(1685): at com.google.android.gms.internal.po(Unknown Source) 06-12 00:40:40.173: E/AndroidRuntime(1685): at com.google.android.gms.internal.bj.a(Unknown Source) 06-12 00:40:40.173: E/AndroidRuntime(1685): at com.google.android.gms.games.GamesClient.submitScore(Unknown Source) 06-12 00:40:40.173: E/AndroidRuntime(1685): at matt.lyons.bibletrivia.lite.MainMenu$8.onClick(MainMenu.java:173) 06-12 00:40:40.173: E/AndroidRuntime(1685): at android.view.View.performClick(View.java:4204) 06-12 00:40:40.173: E/AndroidRuntime(1685): at android.view.View$PerformClick.run(View.java:17355) 06-12 00:40:40.173: E/AndroidRuntime(1685): at android.os.Handler.handleCallback(Handler.java:725) 06-12 00:40:40.173: E/AndroidRuntime(1685): at android.os.Handler.dispatchMessage(Handler.java:92) 06-12 00:40:40.173: E/AndroidRuntime(1685): at android.os.Looper.loop(Looper.java:137) 06-12 00:40:40.173: E/AndroidRuntime(1685): at android.app.ActivityThread.main(ActivityThread.java:5041) 06-12 00:40:40.173: E/AndroidRuntime(1685): at java.lang.reflect.Method.invokeNative(Native Method) 06-12 00:40:40.173: E/AndroidRuntime(1685): at java.lang.reflect.Method.invoke(Method.java:511) 06-12 00:40:40.173: E/AndroidRuntime(1685): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 06-12 00:40:40.173: E/AndroidRuntime(1685): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-12 00:40:40.173: E/AndroidRuntime(1685): at dalvik.system.NativeStart.main(Native Method) 

MainMenu.java

 public class MainMenu extends BaseGameActivity { DatabaseHelper dh; GamesClient client; Context c; @Override public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.mainmenu); client = getGamesClient(); client.connect(); c = this; dh = new DatabaseHelper(this); dh.openDB(); importbutton = (Button)findViewById(R.id.importbutton); importbutton.setOnClickListener(new OnClickListener() { public void onClick(View v) { importScores(); } }); } public void importScores() { final Dialog dialog = new Dialog(c); dialog.setContentView(R.layout.importlayout); dialog.setTitle(R.string.importtitle); TextView question = (TextView)dialog.findViewById(R.id.question); Button save = (Button)dialog.findViewById(R.id.save); Button scratch = (Button)dialog.findViewById(R.id.scratch); question.setText(c.getResources().getString(R.string.importquestion)); save.setText(c.getResources().getString(R.string.savebtn)); scratch.setText(c.getResources().getString(R.string.scratchbtn)); save.setOnClickListener(new OnClickListener() { public void onClick(View v) { long highestJC = dh.getHighestJC(); client.submitScore(c.getResources().getString(R.string.leaderboardjc), highestJC); long highestTenC = dh.getHighestTenC(); client.submitScore(c.getResources().getString(R.string.leaderboardtenc), highestTenC); long highestExodus = dh.getHighestExodus(); client.submitScore(c.getResources().getString(R.string.leaderboardexodus), highestExodus); long highestGenesis = dh.getHighestGenesis(); client.submitScore(c.getResources().getString(R.string.leaderboardgenesis), highestGenesis); long highestHolydays = dh.getHighestHolydays(); client.submitScore(c.getResources().getString(R.string.leaderboardholydays), highestHolydays); long highestFacts = dh.getHighestFacts(); client.submitScore(c.getResources().getString(R.string.leaderboardfacts), highestFacts); long highestActs = dh.getHighestActs(); client.submitScore(c.getResources().getString(R.string.leaderboardacts), highestActs); long highestRandom = dh.getHighestRandom(); client.submitScore(c.getResources().getString(R.string.leaderboardrandom), highestRandom); long highestAll = dh.getHighestAll(); client.submitScore(c.getResources().getString(R.string.leaderboardallcats), highestAll); dialog.dismiss(); } }); scratch.setOnClickListener(new OnClickListener() { public void onClick(View v) { dh.deleteAll(); for(int i = 0; i < 15; i++) { dh.insert(0, 0, "-"); } dialog.dismiss(); dh.closeDB(); } }); dialog.show(); } } 
+4
source share
2 answers

You are trying to send points before establishing a connection.

Like the error, you must wait for onConnected (), and only then you must allow the submission of points. Example: you should only show the button after connecting it.

If you extend BaseGameActivity, you should do something like this:

 int isGooglePlayServiceAvilable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); if (isGooglePlayServiceAvilable == ConnectionResult.SUCCESS) { beginUserInitiatedSignIn(); } else { GooglePlayServicesUtil.getErrorDialog(isGooglePlayServiceAvilable, MainMenu.this, REQUEST_DIALOG).show(); } 

And then you have to override these methods:

 @Override public void onSignInSucceeded() { super.onSignInSucceeded(); // allow to submit scores } @Override public void onSignInFailed() { super.onSignInFailed(); // do not allow to submit scores } 
+3
source

If you are using BaseGameActivity , do not call GamesClient.connect() . The advantage of using BaseGameActivity is that it handles all connection patterns for you. All you have to do is override onSignInSucceeded and call API calls from there. Do not make any game API calls before you get onSignInSucceeded .

Also remember that when your activity receives onStop , the game API will be disabled. After that, when it subsequently receives onStart , you will again wait onSignInSucceeded before onSignInSucceeded any API calls.

+4
source

Source: https://habr.com/ru/post/1485715/


All Articles