How Can I Keep Status 'online' In Android
Solution 1:
Put this dependency in your build.gradle
file:
implementation "android.arch.lifecycle:extensions:*"
Then in your Application class, use this:
public class MyApplication extends Application implements LifecycleObserver {
@Override
public void onCreate() {
super.onCreate();
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
private void onAppBackgrounded() {
Log.d("MyApp", "App in background");
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
private void onAppForegrounded() {
Log.d("MyApp", "App in foreground");
}
}
Update your AndroidManifest.xml file:
<application
android:name=".MyApplication"
....>
</application>
When your app is in background change status to
offline
and when app is in foreground change it's status toonline
.
Solution 2:
The problems look like your ChatsActivity
is destroyed while Glide is trying to load the image. You can use getApplicationContext()
to get the current Context.
Try to replace,
Glide.with(ChatsActivity.this).load(user.getImageurl()).into(image_profile);
with
Glide.with(getApplicationContext()).load(user.getImageurl()).into(image_profile);
Also, you should use .onDisconnect()
method given by firebase to check the presence of the user
When you establish an onDisconnect() operation, the operation lives on the Firebase Realtime Database server. The server checks security to make sure the user can perform the write event requested, and informs the your app if it is invalid. The server then monitors the connection. If at any point the connection times out, or is actively closed by the Realtime Database client, the server checks security a second time (to make sure the operation is still valid) and then invokes the event.
You can find out more from this doc
Solution 3:
Whenever user is in your app, technically he is online. So in your home activity use this:
@Override
protected void onStart() {
super.onStart();
myRef.child(uid).child("isOnline").setValue(true);
myRef.child(uid).child("isOnline").onDisconnect().setValue(false);
}
and also i would suggest to add to override onResume():
@Override
protected void onResume() {
super.onResume();
myRef.child(uid).child("isOnline").setValue(true); //User back online
}
Make sure to add an if statement to check if user is connected to internet or not!
Post a Comment for "How Can I Keep Status 'online' In Android"