Google+ 无法正常登录
Google+ sign in not working correctly
我一直在关注 this guide 使用 google+ 登录并 google 从 android 开发者页面玩游戏。但是,当我的应用程序启动时,我需要按两次 google+ 按钮才能完全登录。我第一次按它时它会询问我想使用哪个帐户(有两个 google accounts on my device),我第二次按下它时,它会在我的屏幕顶部显示 "welcome" 弹出窗口(google play player level)。另外,当我按下注销按钮时,应用程序再次显示登录按钮,但是当我按下登录按钮时,它不会再次登录。
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);
}
}
}
我的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_OK
时,我们正在调用 mGoogleApiClient.connect()
,这与您描述的手动第二次单击具有相同的效果。
我一直在关注 this guide 使用 google+ 登录并 google 从 android 开发者页面玩游戏。但是,当我的应用程序启动时,我需要按两次 google+ 按钮才能完全登录。我第一次按它时它会询问我想使用哪个帐户(有两个 google accounts on my device),我第二次按下它时,它会在我的屏幕顶部显示 "welcome" 弹出窗口(google play player level)。另外,当我按下注销按钮时,应用程序再次显示登录按钮,但是当我按下登录按钮时,它不会再次登录。
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);
}
}
}
我的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_OK
时,我们正在调用 mGoogleApiClient.connect()
,这与您描述的手动第二次单击具有相同的效果。