持续 Google 登录 Android 工作室
Persistent Google Sign In Android Studio
我成功地设置了 google 登录到我的应用程序并且它工作正常。我还添加了一个有效的注销按钮。我面临的问题是,每当我打开该应用程序时,它都要求用户登录 google(或只需按下按钮),然后再转到下一个 activity
.我尝试了很多不同的方法来解决这个问题,但 none 似乎有效。有人可以帮我做一个持久的 google 登录,即使用户关闭应用程序,它也会让他们保持登录状态,而且他们永远不会看到登录 activity(除非他们退出)?
这是我的代码 LoginActivity
:
public class LoginActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
SignInButton signInButton;
private GoogleApiClient googleApiClient;
private SharedPreferences prefs;
private int progress;
private int SIGN_IN;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
googleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso).build();
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
progress = prefs.getInt("SIGN_IN", 0);
signInButton = findViewById(R.id.signInWithGoogle);
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
startActivityForResult(intent, SIGN_IN);
SharedPreferences.Editor editPrefs = prefs.edit();
editPrefs.putInt("SIGN_IN", 1);
editPrefs.apply();
}
});
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == SIGN_IN){
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()){
startActivity(new Intent(LoginActivity.this, StudentInformationFormActivity.class));
finish();
} else {
Toast.makeText(this, "Login Failed!", Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onPause() {
super.onPause();
}
}
我曾经遇到过同样的问题,我查看了我的旧代码,看到了这样的事情:
@Override
protected void onStart() {
super.onStart();
GoogleSignInAccount lastSignedInAccount = GoogleSignIn.getLastSignedInAccount(this);
if (lastSignedInAccount != null) {
startActivity(new Intent(LogInActivity.this, MainActivity.class));
finish();
}
}
因此您可以检查用户是否登录到 Actovity 的 onStart,如果是,则转到下一个 activity。希望有所帮助:)
我成功地设置了 google 登录到我的应用程序并且它工作正常。我还添加了一个有效的注销按钮。我面临的问题是,每当我打开该应用程序时,它都要求用户登录 google(或只需按下按钮),然后再转到下一个 activity
.我尝试了很多不同的方法来解决这个问题,但 none 似乎有效。有人可以帮我做一个持久的 google 登录,即使用户关闭应用程序,它也会让他们保持登录状态,而且他们永远不会看到登录 activity(除非他们退出)?
这是我的代码 LoginActivity
:
public class LoginActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
SignInButton signInButton;
private GoogleApiClient googleApiClient;
private SharedPreferences prefs;
private int progress;
private int SIGN_IN;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
googleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso).build();
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
progress = prefs.getInt("SIGN_IN", 0);
signInButton = findViewById(R.id.signInWithGoogle);
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
startActivityForResult(intent, SIGN_IN);
SharedPreferences.Editor editPrefs = prefs.edit();
editPrefs.putInt("SIGN_IN", 1);
editPrefs.apply();
}
});
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == SIGN_IN){
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()){
startActivity(new Intent(LoginActivity.this, StudentInformationFormActivity.class));
finish();
} else {
Toast.makeText(this, "Login Failed!", Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onPause() {
super.onPause();
}
}
我曾经遇到过同样的问题,我查看了我的旧代码,看到了这样的事情:
@Override
protected void onStart() {
super.onStart();
GoogleSignInAccount lastSignedInAccount = GoogleSignIn.getLastSignedInAccount(this);
if (lastSignedInAccount != null) {
startActivity(new Intent(LogInActivity.this, MainActivity.class));
finish();
}
}
因此您可以检查用户是否登录到 Actovity 的 onStart,如果是,则转到下一个 activity。希望有所帮助:)