Google+ 登录无法正常工作
Posted
技术标签:
【中文标题】Google+ 登录无法正常工作【英文标题】:Google+ sign in not working correctly 【发布时间】:2015-03-18 10:57:01 【问题描述】:我一直在关注this guide 登录 google+ 并从 android 开发者页面上玩 google 游戏。但是,当我的应用程序启动时,我需要按两次 google+ 按钮才能完全登录。第一次按它时会询问我要使用哪个帐户(我的设备上有两个 google 帐户),第二次按当我按下它时,它会在我的屏幕顶部显示“欢迎”弹出窗口(带有 google play 播放器级别)。另外,当我按下注销按钮时,应用程序会再次显示登录按钮,但是当我按下登录按钮时,它不会再次登录。
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_
android:layout_ 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_
android:layout_ />
<!-- sign-out button -->
<Button
android:id="@+id/sign_out_button"
android:layout_
android:layout_
android:text="Sign Out"
android:visibility="gone" />
</RelativeLayout>
非常感谢任何帮助!
【问题讨论】:
你可以看到这个请androidhive.info/2014/02/… 我已经看过很多例子,包括来自 google 的例子。但是,我似乎无法弄清楚我的登录/注销代码与我看到的示例有何不同。另外,使用Log.d
,我可以确定在适当的时刻达到了连接和断开功能。
我已经下载了链接中的源代码并编译了它,但是点击登录按钮后没有任何反应..
【参考方案1】:
您忘记实现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()
,这与您描述的手动第二次单击具有相同的效果。
【讨论】:
以上是关于Google+ 登录无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章
在 PlayStore 上发布后,Google 登录服务无法正常工作
在具有两个 sha1 密钥的 Play 商店中发布后,Google 登录无法正常工作