2015-01-20 7 views
2

나는 Android 개발자 페이지에서 google + 및 google 게임으로 로그인하기 위해 this guide을 팔로우하고 있습니다. 그러나 내 앱이 시작되면 완전히 로그인하기 전에 google + 버튼을 두 번 눌러야합니다. 처음 눌러서 사용하려는 계정 (내 기기에는 두 개의 Google 계정이 있음)과 두 번째 계정 시간에 그것을 누르면 내 화면 상단에 "환영"팝업이 표시됩니다 (Google 재생 플레이어 수준 사용). 또한 로그 아웃 버튼을 누르면 앱에 로그인 버튼이 다시 표시되지만 로그인 버튼을 누르면 다시 로그인하지 않습니다.Google+ 로그인이 올바르게 작동하지 않습니다.

TL : DR : 로그인 버튼을 두 번 눌러 로그인하면 로그 아웃 한 후 로그인 버튼이 더 이상 작동하지 않습니다.

내 MainActivity는 :

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> 

은 어떤 도움이 크게 감사합니다!

+0

이것을 볼 수 있습니다. http://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/ –

+0

나는 이미 많은 예제를 보았습니다. 구글. 그러나 로그인/로그 아웃 코드가 내가 본 예와 다른 점을 파악할 수없는 것 같습니다. 또한'Log.d'를 사용하여 적절한 순간에 연결 및 연결 해제 기능에 도달 할 수 있습니다. – SubliemeSiem

+0

링크에 소스를 다운로드하고 컴파일했지만 로그인 버튼을 클릭해도 아무런 변화가 없습니다. – SubliemeSiem

답변

1

을 구현하여 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()를 호출 반환 될 때.

관련 문제