[英]Saving user credentials from google sign in with Android?
好。 我在我的Android应用程序中集成了Google登录功能。 这是我的登录活动
public class LoginActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
//button
private SignInButton signInButton;
//options
private GoogleSignInOptions gso;
//client api
private GoogleApiClient mGoogleApiClient;
private static final int LCD = 4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_login);
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();
signInButton = (SignInButton) findViewById(R.id.sign_in_button);
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(intent,LCD);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == LCD){
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
//check if the operation is successful
if(result.isSuccess()){
goMainScreen();
}else {
Toast.makeText(this,"Something went wrong",Toast.LENGTH_SHORT).show();
}
}
private void goMainScreen() {
Intent secondActivity = new Intent(this, ProfileActivity.class);
secondActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(secondActivity);
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
登录成功后,我打开Profile活动,并获取用户图像和名称ProfileActivity
public class ProfileActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
private ImageView photo;
private TextView name;
private GoogleApiClient googleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
photo = (ImageView) findViewById(R.id.profileImage);
name = (TextView) findViewById(R.id.theName);
GoogleSignInOptions gsp = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
googleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this,this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gsp)
.build();
}
@Override
protected void onStart() {
super.onStart();
OptionalPendingResult<GoogleSignInResult> optionalPendingResult = Auth.GoogleSignInApi.silentSignIn(googleApiClient);
if(optionalPendingResult.isDone()){
GoogleSignInResult sig = optionalPendingResult.get();
handleSigninResult(sig);
}else {
optionalPendingResult.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(@NonNull GoogleSignInResult googleSignInResult) {
handleSigninResult(googleSignInResult);
}
});
}
}
private void handleSigninResult(GoogleSignInResult sig) {
if(sig.isSuccess()){
GoogleSignInAccount acc = sig.getSignInAccount();
//accessing the data
name.setText(acc.getDisplayName());
//image with glide
Glide.with(this).load(acc.getPhotoUrl()).into(photo);
}else {
//in case is not successful
//send the user to the Login Screen
goLoginInScree();
}
}
private void goLoginInScree() {
Intent goUser = new Intent(this, LoginActivity.class);
goUser.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(goUser);
}
@Override
public void onConnectionFailed( ConnectionResult connectionResult) {
}
我的下一个问题是,如果用户已经登录到活动中,该如何实现自动登录功能? 我不希望每次打开应用程序时都单击“登录”按钮,而是直接转到个人资料活动。
在Google身份验证中成功登录后,将结果存储在共享首选项中。
private void handleSigninResult(GoogleSignInResult sig) {
if(sig.isSuccess()){
GoogleSignInAccount acc = sig.getSignInAccount();
//accessing the data
name.setText(acc.getDisplayName());
SharedPreferences prefs = getSharedPreferences("shared_pref_name", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("email", acc.getEmail());
editor.putInt("name", acc.getDisplayName());
editor.putBoolean("hasLogin",true);
editor.apply();
//image with glide
Glide.with(this).load(acc.getPhotoUrl()).into(photo);
}else {
//in case is not successful
//send the user to the Login Screen
goLoginInScree();
}
}
注意:用户注销时清除共享首选项
最简单的方法是使用SharedPreferences
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
使用Google+成功登录后,将凭据详细信息保存在Sharedpreferences中
SharedPreferences.Editor editor = prefs.edit();
editor.putString("email", acc.getEmail());
editor.putInt("name", acc.getDisplayName());
editor.putBoolean("hasLogin",true); // set the prefs true after success login
editor.apply();
然后,您可以检查用户是否是首次登录
if(prefs.getBoolean("hasLogin")){
//no need to sigin again.. proceeed to your activity
}
else{
//need to sign in
}
如果用户要注销,只需撤销Access google登录即可,包括将首选项“ hasLogin”设置为“ false”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.