即使用户在android studio中关闭应用程序,如何保持登录状态
Posted
技术标签:
【中文标题】即使用户在android studio中关闭应用程序,如何保持登录状态【英文标题】:How to remain signed in even when the user closes the app in android studio 【发布时间】:2021-09-06 02:42:08 【问题描述】:一开始我在注销时遇到了问题 (link:How to logout a user when he wants to in android studio) 。我删除了即使在应用程序关闭后也有助于保持用户登录的部分,现在注销工作正常。但现在我的问题是我应该如何让用户保持登录状态? 这是我的登录页面
public class Login_Page extends AppCompatActivity
ActivityLoginPageBinding binding;
ProgressDialog progressdialog;
FirebaseAuth auth;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
binding = ActivityLoginPageBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
button = findViewById(R.id.Googlelogin);
button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(Login_Page.this, SignUp.class);
startActivity(intent);
//taking the user to signup page
);
auth = FirebaseAuth.getInstance();
progressdialog = new ProgressDialog(Login_Page.this);
progressdialog.setTitle("Logging in");
progressdialog.setMessage("Please wait");
binding.Login.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
progressdialog.show();
auth.signInWithEmailAndPassword(binding.Email.getText().toString(), binding.Password.getText().toString())
.addOnCompleteListener(new OnCompleteListener<AuthResult>()
@Override
public void onComplete(@NonNull Task<AuthResult> task)
progressdialog.dismiss();
if (task.isSuccessful())
Toast.makeText(Login_Page.this, "Success", Toast.LENGTH_SHORT).show();
String uid = auth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users").child("Driver").child(uid);
ValueEventListener valueEventListener = new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot snapshot)
if (snapshot.exists())
Toast.makeText(Login_Page.this, "LoginDriver", Toast.LENGTH_SHORT);
Intent intent = new Intent(Login_Page.this, HomePage.class);
startActivity(intent);
else
startActivity(new Intent(Login_Page.this, Custhomepage.class));
@Override
public void onCancelled(@NonNull DatabaseError error)
;
uidRef.addListenerForSingleValueEvent(valueEventListener);
else
Toast.makeText(Login_Page.this, task.getException().getMessage(), Toast.LENGTH_SHORT);
);
);
【问题讨论】:
使用身份验证侦听器,就像我对这个post 的回答一样。 【参考方案1】:我猜你没有检查用户是否登录。在activity的onCreate中,调用下面的方法checkFirebaseUser()
private void checkFirebaseUser ()
FirebaseUser firebaseUser = mAuth.getCurrentUser ();
if (firebaseUser != null)
//User is logged in already. You can proceed with your next screen
else
//User not logged in. So show email and password edit text for user to enter credentials
希望上面的代码sn-p对你有帮助!
【讨论】:
嘿,我试过了,但是当我尝试注销时它开始产生问题 嘿@MohammadYusuf 看起来在注销时出现问题。我在上述链接中发布了针对您的问题的解决方案。希望对你有帮助以上是关于即使用户在android studio中关闭应用程序,如何保持登录状态的主要内容,如果未能解决你的问题,请参考以下文章
即使在 Android 4.4.2 中关闭了位置服务,GetCurrentLocation 仍在 Cordova/PhoneGap 应用程序上工作
在Android Studio中关闭手机上的应用程序时如何不关闭套接字