How can i keep status 'online' in android

前端 未结 3 1659
我在风中等你
我在风中等你 2020-12-22 01:17

Now i\'m trying to make like a instagram chat program using firebase the problem is i want to show that member login status but android life cycle is problem

Here

相关标签:
3条回答
  • 2020-12-22 01:49

    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!

    0 讨论(0)
  • 2020-12-22 01:57

    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

    0 讨论(0)
  • 2020-12-22 02:11

    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 to online.

    Reference

    0 讨论(0)
提交回复
热议问题