[英]How to update a field when my app is killed(Firebase)
I have an apps where I need to track my user active or not.我有一个应用程序需要跟踪我的用户是否活跃。 So I create a service class to update user active status.Here is my service class code
所以我创建了一个服务 class 来更新用户活动状态。这是我的服务 class 代码
class ServiceClass : Service() {
override fun onBind(p0: Intent?): IBinder? {
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Repository.updateActiveStatus(true)
return START_NOT_STICKY
}
override fun onDestroy() {
super.onDestroy()
Repository.updateActiveStatus(false)
}
override fun onTaskRemoved(rootIntent: Intent?) {
super.onTaskRemoved(rootIntent)
Log.d("TAGService", "onTaskRemoved: false")
Firebase.firestore.collection("User").document(Firebase.auth.uid.toString())
.update("active", true).addOnSuccessListener {
Log.d("TAGService", "onTaskRemoved: updated")
}
stopSelf()
}}
here is my manifest code这是我的清单代码
<service android:name=".utils.ServiceClass" android:stopWithTask="false"></service>
But when I open my apps its update its active status but when I destroy or killed my code its not updating.但是,当我打开我的应用程序时,它会更新其活动状态,但当我销毁或终止我的代码时,它不会更新。
If you want to check user is active or not then it better to check at application level instead of service level.如果您想检查用户是否处于活动状态,那么最好在应用程序级别而不是服务级别进行检查。 And if you like to update the UI you can try Event bus .
如果你想更新 UI,你可以试试Event bus 。 Here is the sample code.
这是示例代码。
public class AppOpenManager :Application.ActivityLifecycleCallbacks, LifecycleObserver{
.....
@OnLifecycleEvent(ON_START)
public fun onStart() {
Log.d(LOG_TAG, "onStart");
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public fun onAppBackgrounded() {
Log.d(LOG_TAG,"APP BACKGROUNDED");
}
....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.