私はしばらくこれに取り組んできました-そして、ここで回答を広範囲に検索しましたが、何も見つかりませんでした(解決策を逃した場合は申し訳ありません)。「Googleでサインイン」ボタン/ロジックを実装していますが、デバイスの向きを変更する場合を除いて、すべて正常に動作します。その場合、Google の権限ダイアログの複数のコピーが表示されます。(つまり、向きを 3 回変更すると、元の画面に戻る前にキャンセルしなければならないアクセス許可ダイアログのコピーが 3 つ存在します)。(権限ダイアログの例は、ここにあります)。
コードを複雑にしすぎたと思ったので、Google のチュートリアル ページ ( Getting Started/Sign-In ) のコードだけを使用して新しいアクティビティを作成しましたが、まだ同じ問題が発生しています。(以下のコード)
(FWIW、IntelliJの「新規->アクティビティ->ログインアクティビティ」オプションを使用して新しいアクティビティを作成しようとしましたが、同じ結果が得られました。)
それに加えて、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();
}
}
}
}