我设计了一个来自** firebase 的登录界面,我希望用户只登录一次,然后忽略登录界面。**
公共类MainActivity扩展AppCompatActivity实现了GoogleApiClient.OnConnectionFailedListener,View.OnClickListener {
SignInButton signInButton;
private FirebaseAuth mAuth;
GoogleApiClient mGoogleApiClient;
private static final int RC_SING_IN = 9001;
private static final String TAG = "SingInActivity";
private TextView textView;
private ImageView imgAvatar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
// Configure Google Sign In
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();
signInButton = (SignInButton) findViewById(R.id.busingin);
signInButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.busingin:
singIn();
break;
}
}
private void singIn() {
Intent singInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(singInIntent, RC_SING_IN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SING_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult : " + result.isSuccess());
if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
startActivity(new Intent(MainActivity.this, Main2Activity.class));
firebaseAuthWithGoogle(acct);
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.d(TAG, "onConnecationFailed :" + connectionResult);
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d("TECSTORE", "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("TECSTORE", "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w("TECSTORE", "signInWithCredential", task.getException());
}
}
});
}
} 我从 firebase 设计了一个登录界面,我希望用户只登录一次,然后忽略登录界面。
答案 0 :(得分:0)
您可以使用sharedpreference
。
首先,当用户第一次输入凭据时,您将进入sharedpreference
。
//Create login session
private static final String IS_USER_LOGIN = "IsUserLoggedIn";
public void createUserLoginSession(String name, String email){
// Storing login value as TRUE
editor.putBoolean(IS_USER_LOGIN, true);
// Storing name in pref
editor.putString(KEY_NAME, name);
// Storing email in pref
editor.putString(KEY_EMAIL, email);
// commit changes
editor.commit();
}
下次检查用户是否存在
// Check for login
public boolean isUserLoggedIn(){
return pref.getBoolean(IS_USER_LOGIN, false);
}
public boolean checkLogin(){
// Check login status
if(!this.isUserLoggedIn()){
// user is not logged in redirect him to Login Activity
Intent i = new Intent(YourCurrentActivity.this, LoginActivity.class);
// Closing all the Activities from stack
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
startActivity(i);
return true;
}
return false;
}