简体   繁体   中英

Google Play Game Services & libGDX “SignInActivity must be started with startActivityForResult”

I'm trying to implement Google Play Game Services into my libGDX game. I followed the tutorial here: http://helios.hud.ac.uk/u1070589/blog/?p=202

When my game loads, I create a new GameHelper object and call its setup method. I've debugged this part and it completes without any problems. I've placed a GPGS button on my main menu, when it's clicked I call the Login() method in my main Android Activity.

At this point the mConnectionResult.startResolutionForResult(mActivity, RC_RESOLVE) call in the resolveConnectionResult method (GameHelper class) returns the error:

E/SignInActivity(21930): SignInActivity must be started with startActivityForResult

The ConnectionResult object (mConnectionResult) doesn't have a public method available called startActivityForResult so I can't just change the startResolutionForResult call.

In the beginUserInitiatedSignIn() method (GameHelper) the connection returns ConnectionResult.SUCCESS . When resolveConnectionResult() is called, mConnectionResult.hasResolution() also returns true.

I've double-checked that my debug key matches the entry for my app in the Google APIs Console. I've checked that my app ID matches the one shown on the Google Play Developer Console.

I have managed to get the Type-a-Number example app working without any problems. Here's my main Android activity:

package com.eb.droid;

import android.content.Intent;
import android.view.Window;
import android.view.WindowManager;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.swarmconnect.Swarm;

import com.eb.GoogleInterface;
import com.google.example.games.basegameutils.GameHelper;
import com.google.example.games.basegameutils.GameHelper.GameHelperListener;

public final class AndroidGame extends AndroidApplication implements GameHelperListener, GoogleInterface
{   
    private final int RC_RESOLVE = 5000, RC_UNUSED = 5001; //request codes we use when invoking an external activity
    private final SwarmData swarmData = new SwarmData();
    private GameHelper aHelper;

    public AndroidGame()
    {
        aHelper = new GameHelper(this);
        aHelper.enableDebugLog(true, "MYTAG");
    }

    public final void onCreate(android.os.Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();

        // Disable hardware functions to save battery power.        
        cfg.useAccelerometer = false;
        cfg.useCompass = false;

        cfg.useGL20 = true;
        aHelper.setup(this);
        setSwarmKeyPartA();
        initialize(new Game(this, swarmData), cfg);

        // Activate Swarm if user is already logged-in. App won't nag player to log-in, they always have to 
        // do it manually via the Swarn dashboard via the title screen.
        //Log.d(LOG, "onCreate() Swarm.isLoggedIn() = " + Swarm.isLoggedIn());
        if (Swarm.isLoggedIn())
        {
            Swarm.setActive(this);
        }
    }

    public void onResume()
    {
        super.onResume();
        if (Swarm.isLoggedIn())
        {
            Swarm.setActive(this);
            Swarm.init(this, swarmData.swarmAppID, swarmData.swarmAppKey);
        }
    }

    public void onPause()
    {
        super.onPause();
        Swarm.setInactive(this);
    }

    @Override
    ublic void onStart()
    {
        super.onStart();
        aHelper.onStart(this);
    }

    @Override
    public void onStop()
    {
        super.onStop();
        aHelper.onStop();
    }

    @Override
    public void onActivityResult(int request, int response, Intent data)
    {
        super.onActivityResult(request, response, data);
        aHelper.onActivityResult(request, response, data);
    }

    public void onSignInFailed()
    {
        System.out.println("sign in failed");
    }

    public void onSignInSucceeded()
    {
        System.out.println("sign in succeeded");
    }

    public void Login()
    {       
        try
        {
            runOnUiThread(new Runnable()
            {
                @Override
                public void run()
                {
                    aHelper.beginUserInitiatedSignIn();
                }
            });
        }
        catch (final Exception ex)
        {

        }
    }

    public void LogOut()
    {
        try
        {
            runOnUiThread(new Runnable()
            {
                @Override
                public void run()
                {
                    aHelper.signOut();
                }
            });
        }
        catch (final Exception ex)
        {

        }
    }

    public boolean getSignedIn()
    {
        return aHelper.isSignedIn();
    }

    public void submitScore(int _score)
    {
        System.out.println("in submit score");
        aHelper.getGamesClient().submitScore(getString(R.string.leaderboard_high_scores), _score);
    }

    public void showAchievements()
    {
        startActivityForResult(aHelper.getGamesClient().getAchievementsIntent(), RC_UNUSED);
    }

    public void showLeaderboards()
    {
        startActivityForResult(aHelper.getGamesClient().getAllLeaderboardsIntent(), RC_UNUSED);
    }

    public void getScoresData()
    {

    }
}

My app also currently implements Swarm, as can be seen in the Activity. I tried removing Swarm to see if that was negatively affecting the GPGS login, but still no luck.

I had the same issue, then I noticed this in the logs: "Activity is launching as a new task, so cancelling activity result"

In the AndroidManifest.xml file of my libgdx game, I had set this property: 'android:launchMode="singleInstance"' As soon as I removed that, I was able to use GPGS just like the sample apps.

I haven't looked at your code in detail. Basically I used the supplied GameHelper (I think you have done the same) and I put everything I needed from BaseGameUtils in my fragments. I suggest you look at a successful trace from TypeANumber to see what you are missing. Here is one of my traces:

07-04 10:21:54.511: D/ian_(1781): MultiTab3 beginUserInitiatedSignIn
04 10:21:54.531: D/ian_(1781): isGooglePlayServicesAvailable returned 0
07-04 10:21:54.531: D/ian_(1781): beginUserInitiatedSignIn: continuing pending sign-in flow.
07-04 10:21:54.611: D/ian_(1781): resolveConnectionResult: trying to resolve result: C      onnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{40f3ed38: android.os.BinderProxy@40ee3de0}}
07-04 10:21:54.611: D/ian_(1781): result has resolution. Starting it.
07-04 10:21:54.621: D/ian_(1781): startResolutionForResult - this may be prob ?
07-04 10:23:29.480: D/ian_(1781): MultiPlayer onActivityResult called9001-1null
07-04 10:23:29.520: D/ian_(1781): MultiPlayer passing onActivityResult to MultiTab3 Req/Resp/Data=9001-1null
07-04 10:23:29.520: D/ian_(1781): MultiTab3 onActivityResult - passing through to GameHelper ...9001-1null
07-04 10:23:29.520: D/ian_(1781): onActivityResult, req 9001 response -1
07-04 10:23:29.520: D/ian_(1781): responseCode == RESULT_OK. So connecting.
07-04 10:23:30.130: D/ian_(1781): onConnected: connected! client=1
07-04 10:23:30.130: D/ian_(1781): All clients now connected. Sign-in successful.
07-04 10:23:30.130: D/ian_(1781): All requested clients connected. Sign-in succeeded!

Update:

What about this - is this just a copy and paste error or is your program missing the "P" on Public ? (You do need to make this call)

    @Override
    ublic void onStart()
    {
        super.onStart();
        aHelper.onStart(this);
    }

Update 2:

Based on the work you have done, you have now eliminated the possibility of a programming error. I have not seen a setup error that caused this problem. However I did encounter this which is (sort of) similar:

onConnectionFailed: result 4
onConnectionFailed: since user initiated sign-in, trying to resolve problem
statusCode=SIGN_IN_REQUIRED resolution=PendingIntent
result has resolution. Starting it.

Explanation from google docs "The client attempted to connect to the service but the   user is not signed in"

n.b. Google setting is showing that we are signed in as ..........@gmail.com

further investigation **suggests** that this may be because of a set up problem in the 
google play api console and/or google play developer console ...

I also notice that you mentioned the Google API console. I suggest you create a new set of definitions in the Developer Console and that you do not make any amendments through the API console. I am not saying that you have changed things in the API console - I just think it is easier at this point to create a new set of definitions and (I'm repeating myself I know) don't make any changes through the API console .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM