2017-03-07 29 views
-1

我想將Google登錄集成到我的遊戲中。但是我不想讓用戶打開應用程序時使用按鈕進行登錄。 每當創建MenuActivity時,它都會要求選擇一個帳戶進行登錄。但是,我希望它只選擇一次帳戶(第一次)並記住每次。這裏是代碼:Google+登錄要求每次在創建活動時選擇帳戶

public class MenuActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener { 

private GoogleApiClient mGoogleApiClient; 
private static final String TAG = MainActivity.class.getSimpleName(); 
private static final int RC_SIGN_IN = 007; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_menu); 

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
      .requestEmail() 
      .build(); 

    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .enableAutoManage(this, this) 
      .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
      .build(); 



    if(!mGoogleApiClient.isConnected()){ 
     signIn(); 
    } 
} 

public void startGame(View view){ 
    startActivity(new Intent(getApplicationContext(), MainActivity.class)); 
} 

private void signIn() { 
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
    startActivityForResult(signInIntent, RC_SIGN_IN); 
} 



@Override 
protected void onStart() { 
    super.onStart(); 
    mGoogleApiClient.connect(); 

} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); 
    if (requestCode == RC_SIGN_IN) { 
     GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
     handleSignInResult(result); 
    } 
} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
    // An unresolvable error has occurred and Google APIs (including Sign-In) will not 
    // be available. 
    Log.d(TAG, "onConnectionFailed:" + connectionResult); 
} 

private void handleSignInResult(GoogleSignInResult result) { 
    Log.d(TAG, "handleSignInResult:" + result.isSuccess()); 
    if (result.isSuccess()) { 
     // Signed in successfully, show authenticated UI. 
     GoogleSignInAccount acct = result.getSignInAccount(); 

    } else { 

    } 
} 
} 

回答

0

首先創建SharedPreferences

public void saveUser (String key, String value) { 
    SharedPreferences pref = getSharedPreferences("YourPref", MODE_PRIVATE); 
    SharedPreferences.Editor editor = pref.edit(); 
    editor.putString(key, value); 
    editor.commit(); 
} 

public String getUser (String key) { 
    SharedPreferences pref = getSharedPreferences("YourPref", MODE_PRIVATE); 
    return pref.getString(key, ""); 
} 

檢查用戶的電子郵件,如果它是空加SharedPreferences

if(!mGoogleApiClient.isConnected() && getUser("email").isEmpty()){ 
    signIn(); 
} 


private void handleSignInResult(GoogleSignInResult result) { 
    Log.d(TAG, "handleSignInResult:" + result.isSuccess()); 
    if (result.isSuccess()) { 
    // Signed in successfully, show authenticated UI. 
     GoogleSignInAccount acct = result.getSignInAccount(); 
     saveUser("email", acct.getEmail()); 
     saveUser("name", acct.getDisplayName()); 
    } else { 

    } 
} 
相關問題