我希望用户在进行任何活动时都可以在线出现在我的应用程序中,直到他们关闭或最小化该应用程序为止。以下是我在MainActivity.java
if (mAuth.getCurrentUser() != null) {
mUserRef = FirebaseDatabase.getInstance().getReference().child("Users").child(mAuth.getCurrentUser().getUid());
}
}
@Override
public void onStart () {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser == null) {
sendtostart();
} else {
mUserRef.child("online").setValue("true");
}
}
@Override
protected void onStop () {
super.onStop();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser != null) {
mUserRef.child("online").setValue(ServerValue.TIMESTAMP);
}
}
private void sendtostart () {
Intent startIntent = new Intent(MainActivity.this, StartActivity.class);
startActivity(startIntent);
finish();
}
,但这仅在用户处于主要活动中时有效,因此当用户切换到“聊天活动”或“个人资料活动”时,它们将显示为脱机状态。以下是我在ChatActivity.java
中尝试过的方法,但是没有用
if (mAuth.getCurrentUser() != null) {
mUserRef = FirebaseDatabase.getInstance().getReference().child("Users").child(mAuth.getCurrentUser().getUid());
}
}
@Override
public void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser != null){
mUserRef.child("online").setValue("true");
}else {
mUserRef.child("online").setValue("false");
}