How can i keep status 'online' in android

前端 未结 3 1670
我在风中等你
我在风中等你 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 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:

    
    
    

    When your app is in background change status to offline and when app is in foreground change it's status to online.

    Reference

提交回复
热议问题