// MainActivity.javapackage...importandroid.content.Intent;importandroid.os.Bundle;importandroid.support.design.widget.FloatingActionButton;importandroid.support.design.widget.Snackbar;importandroid.support.v7.app.AppCompatActivity;importandroid.support.v7.widget.Toolbar;importandroid.util.Log;importandroid.view.View;importandroid.view.Menu;importandroid.view.MenuItem;importcom.google.android.gms.auth.api.Auth;importcom.google.android.gms.auth.api.signin.GoogleSignInAccount;importcom.google.android.gms.auth.api.signin.GoogleSignInOptions;importcom.google.android.gms.auth.api.signin.GoogleSignInResult;importcom.google.android.gms.common.ConnectionResult;importcom.google.android.gms.common.SignInButton;importcom.google.android.gms.common.api.GoogleApiClient;publicclassMainActivityextendsAppCompatActivityimplementsGoogleApiClient.OnConnectionFailedListener,View.OnClickListener{privatestaticfinalStringTAG="MainActivity";privatestaticfinalintRC_SIGN_IN=9001;privateGoogleApiClientmGoogleApiClient;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);<省略>// Button listenersの設定findViewById(R.id.button_sign_in).setOnClickListener(this);// Google Sign-Inの設定GoogleSignInOptionsgso=newGoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();mGoogleApiClient=newGoogleApiClient.Builder(this).enableAutoManage(this/* FragmentActivity */,this/* OnConnectionFailedListener */).addApi(Auth.GOOGLE_SIGN_IN_API,gso).build();// Google Sign-Inボタンの設定SignInButtonsignInButton=(SignInButton)findViewById(R.id.button_sign_in);signInButton.setSize(SignInButton.SIZE_STANDARD);signInButton.setScopes(gso.getScopeArray());}<省略>// Sign-Inした結果を受け取る処理@OverridepublicvoidonActivityResult(intrequestCode,intresultCode,Intentdata){super.onActivityResult(requestCode,resultCode,data);// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);if(requestCode==RC_SIGN_IN){GoogleSignInResultresult=Auth.GoogleSignInApi.getSignInResultFromIntent(data);handleSignInResult(result);}}// Sign-Inした結果を扱う処理privatevoidhandleSignInResult(GoogleSignInResultresult){Log.d(TAG,"handleSignInResult:"+result.isSuccess());if(result.isSuccess()){// Signed in successfully, show authenticated UI.GoogleSignInAccountacct=result.getSignInAccount();}else{// Signed out, show unauthenticated UI.}}// Sign-In処理privatevoidsignIn(){IntentsignInIntent=Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);startActivityForResult(signInIntent,RC_SIGN_IN);}@OverridepublicvoidonClick(Viewv){switch(v.getId()){caseR.id.button_sign_in:signIn();break;}}}