我一直在努力解决这个问题——我在这里广泛搜索了答案,但没有找到任何答案(如果我错过了一个解决方案,请道歉)。我正在实现“使用 Google 登录”按钮/逻辑,除了更改设备方向外,一切正常。在这种情况下,我会看到 Google 权限对话框的多个副本。(因此,如果我更改了 3 次方向,则在返回到原始屏幕之前,我必须取消权限对话框的三个副本)。(可以在此处找到权限对话框的示例)。
我以为我的代码过于复杂,所以我做了一个新活动,只使用了 Google 教程页面(入门/登录)中的代码,但我仍然遇到同样的问题。(代码如下)
(FWIW,我还尝试使用 IntelliJ 的“New --> Activity --> Login Activity”选项创建一个新的 Activity,结果相同。)
除此之外,我尝试运行 Google 的“快速入门”应用程序,是的,同样的问题仍在发生!
有没有其他人在没有这种行为的情况下成功实现了“使用 Google 登录”?我想作为最后的手段,我可以强制我的身份验证活动始终以纵向显示,但我正在尝试查看是否有除此之外的解决方案。
先感谢您!
以下是我的“简化”活动中的代码:
package com.myapp.test.view.housekeeping;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Toast;
import com.myapp.test.R;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.plus.Plus;
public class AuthenticateActivity_BareBones extends ActionBarActivity
implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
/* Request code used to invoke sign in user interactions. */
private static final int RC_SIGN_IN = 1;
/* Client used to interact with Google APIs. */
private GoogleApiClient mGoogleApiClient;
/* A flag indicating that a PendingIntent is in progress and prevents
* us from starting further intents.
*/
private boolean mIntentInProgress;
/* Track whether the sign-in button has been clicked so that we know to resolve
* all issues preventing sign-in without waiting.
*/
private boolean mSignInClicked;
/* Store the connection result from onConnectionFailed callbacks so that we can
* resolve them when the user clicks sign-in.
*/
private ConnectionResult mConnectionResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_authenticate);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RC_SIGN_IN:
if (resultCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
break;
}
}
public void onButtonClick(View view) {
int id = view.getId();
switch (id) {
case R.id.login_authenticate_google_button:
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
break;
}
}
@Override
public void onConnected(Bundle connectionHint) {
// We've resolved any connection errors. mGoogleApiClient can be used to
// access Google APIs on behalf of the user.
mSignInClicked = false;
Toast.makeText(this, "Connected to Google!", Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionSuspended(int cause) {
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (!mIntentInProgress) {
// Store the ConnectionResult so that we can use it later when the user clicks
// 'sign-in'.
mConnectionResult = result;
if (mSignInClicked) {
// The user has already clicked 'sign-in' so we attempt to resolve all
// errors until the user is signed in, or they cancel.
resolveSignInError();
}
}
}
/* A helper method to resolve the current ConnectionResult error. */
private void resolveSignInError() {
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
startIntentSenderForResult(mConnectionResult.getResolution().getIntentSender(),
RC_SIGN_IN, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
// The intent was canceled before it was sent. Return to the default
// state and attempt to connect to get an updated ConnectionResult.
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
}