Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 谷歌+;登录无法正常工作_Android_Login_Google Plus_Google Play Games - Fatal编程技术网

Android 谷歌+;登录无法正常工作

Android 谷歌+;登录无法正常工作,android,login,google-plus,google-play-games,Android,Login,Google Plus,Google Play Games,我一直在关注android开发者页面上的google+和google play游戏。但是,当我的应用程序启动时,我需要按两次google+按钮才能完全登录。我第一次按它时,它会询问我想使用哪个帐户(我的设备上有两个谷歌帐户),第二次按它时,它会在我的屏幕顶部显示“欢迎”弹出窗口(具有谷歌play player级别)。此外,当我按下注销按钮时,应用程序会再次显示登录按钮,但当我按下登录按钮时,它不会再次登录 TL;DR:必须按两次登录按钮才能登录,注销后,登录按钮将不再工作 我的主要活动: pub

我一直在关注android开发者页面上的google+和google play游戏。但是,当我的应用程序启动时,我需要按两次google+按钮才能完全登录。我第一次按它时,它会询问我想使用哪个帐户(我的设备上有两个谷歌帐户),第二次按它时,它会在我的屏幕顶部显示“欢迎”弹出窗口(具有谷歌play player级别)。此外,当我按下注销按钮时,应用程序会再次显示登录按钮,但当我按下登录按钮时,它不会再次登录

TL;DR:必须按两次登录按钮才能登录,注销后,登录按钮将不再工作

我的主要活动:

public class MainActivity extends Activity implements
        View.OnClickListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    public SharedPreferences prefs;

    private static int RC_SIGN_IN = 9001;

    private GoogleApiClient mGoogleApiClient;

    private boolean mResolvingConnectionFailure = false;
    private boolean mAutoStartSignInFlow = true;
    private boolean mSignInClicked = false;

    boolean mExplicitSignOut =true; // set to true since we don't want an automatic login untill the user has done a manual login
    boolean mInSignInFlow = false; // set to true when you're in the middle of the
    // sign in flow, to know you should not attempt
    // to connect in onStart()

    // Every "clickable" item in this application (buttons, etc)
    final static int[] CLICKABLES = {
       R.id.sign_in_button,
       R.id.sign_out_button
    };

    private boolean isSignedIn() {
        return (mGoogleApiClient != null && mGoogleApiClient.isConnected());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // set the onClickListener to every "clickable" view
        for (int id: CLICKABLES) {
            findViewById(id).setOnClickListener(this);
        }

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .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();

    }

    @Override
    public void onConnected(Bundle bundle) {
        // Connected; change GUI accordingly

        // show sign-out button, hide the sign-in button
        findViewById(R.id.sign_in_button).setVisibility(View.GONE);
        findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE);
    }

    @Override
    public void onConnectionSuspended(int i) {
        mGoogleApiClient.connect();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (mResolvingConnectionFailure) {
            // Already resolving
            return;
        }

        // If the sign in button was clicked or if auto sign-in is enabled,
        // launch the sign-in flow
        if (mSignInClicked || mAutoStartSignInFlow) {
            mAutoStartSignInFlow = false;
            mSignInClicked = false;
            mResolvingConnectionFailure = true;

            // Attempt to resolve the connection failure using BaseGameUtils.
            // The R.string.signin_other_error value should reference a generic
            // error string in your strings.xml file, such as "There was
            // an issue with sign in, please try again later."
            if (!BaseGameUtils.resolveConnectionFailure(this,
                    mGoogleApiClient, connectionResult,
                    RC_SIGN_IN, getString(R.string.signin_other_error))) {
                mResolvingConnectionFailure = false;
            }
        }
        findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
        findViewById(R.id.sign_out_button).setVisibility(View.GONE);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!mInSignInFlow && !mExplicitSignOut) {
            // auto sign in
            mGoogleApiClient.connect();
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.sign_in_button) {
            // start the asynchronous sign in flow
            mExplicitSignOut = false;
            mSignInClicked = true;
            mGoogleApiClient.connect();
        }
        else if (view.getId() == R.id.sign_out_button) {
            // sign out.
            mSignInClicked = false;
            // user explicitly signed out, so turn off auto sign in
            mExplicitSignOut = true;
            if (isSignedIn()) {
                Games.signOut(mGoogleApiClient);
                mGoogleApiClient.disconnect();
            }

            // show sign-in button, hide the sign-out button
            findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
            findViewById(R.id.sign_out_button).setVisibility(View.GONE);
        }
    }
}
My activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    <!-- sign-in button -->
    <com.google.android.gms.common.SignInButton
        android:id="@+id/sign_in_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!-- sign-out button -->
    <Button
        android:id="@+id/sign_out_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sign Out"
        android:visibility="gone" />
</RelativeLayout>


非常感谢您的帮助

您忘记实现
onActivityResult
以捕获
BaseGameUtils.resolveConnectionFailure的结果。你会想要这样的东西:

        @Override
        public void onActivityResult(int request, int response, Intent data) {
            super.onActivityResult(request, response, data);
            if (request == RC_SIGN_IN) {
                mSignInClicked = false;
                mResolvingConnectionFailure = false;
                if (response == Activity.RESULT_OK) {
                    mGoogleApiClient.connect();
                } else {
                    BaseGameUtils.showActivityResultError(this, request, response, R.string.signin_other_error);
                }
            }
        }

您可以看到,当返回
RESULT\u OK
时,我们调用的是
mgoogleapclient.connect()
,其效果与您描述的手动第二次单击相同。

您可以看到这一点,请注意,我已经看到了很多示例,包括来自谷歌的示例。然而,我似乎无法找出我的登录/注销代码与我所看到的示例的不同之处。另外,使用
Log.d
,我可以确定连接和断开连接功能是在适当的时候实现的。我已经在链接中下载了源代码并对其进行了编译,但在单击“登录”按钮后,什么也没有发生。。