在主线程中从Firebase Service访问数据 [英] Access data from Firebase Service in main thread
问题描述
我正在编程一个应用程序,该应用程序从FCM消息中接收一些数据,然后根据消息中包含的数据有效载荷更新UI.
I am programming an app that receives some data from a FCM message, then updates the UI based on the data payload contained in the message.
该服务运行良好,并且可以接收数据,但是我无法弄清楚如何将其恢复到我的主要活动中.
The service runs fine, and receives the data, but I cant figure out how to get it back to my main activity.
Firebase服务在我的清单中声明为
The Firebase service is declared in my manifest as
<service android:name=".MyFirebaseService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
在MyFirebaseService类中,我重写了onMessageReceived方法,但是如何通知我的主要活动已收到消息?
In my MyFirebaseService class I have overridden the onMessageReceived method, but how am I supposed to notify my main activity that a message was received?
我可以在Firebase服务中使用处理程序吗?
Is there a handler I can use with the Firebase service?
推荐答案
dev.android.com > Develop > Training > Background Jobs > Reporting Work Status
https://developer.android.com/training /run-background-service/report-status.html
将IntentService中工作请求的状态发送给其他人 组件,首先创建一个Intent,其中包含其状态 扩展数据.
To send the status of a work request in an IntentService to other components, first create an Intent that contains the status in its extended data.
接下来,通过致电发送Intent LocalBroadcastManager.sendBroadcast().这会将Intent发送给任何 您的应用程序中已注册接收该组件的组件.
Next, send the Intent by calling LocalBroadcastManager.sendBroadcast(). This sends the Intent to any component in your application that has registered to receive it.
Intent localIntent = new Intent(BROADCAST_ACTION).putExtra(DATA_STATUS, status);
LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
要接收广播Intent对象,请使用的子类 BroadcastReceiver.在子类中,实现
BroadcastReceiver.onReceive()
回调方法,其中 LocalBroadcastManager在收到Intent时调用. LocalBroadcastManager将传入的Intent传递给BroadcastReceiver.onReceive()
.
To receive broadcast Intent objects, use a subclass of BroadcastReceiver. In the subclass, implement the
BroadcastReceiver.onReceive()
callback method, which LocalBroadcastManager invokes when it receives an Intent. LocalBroadcastManager passes the incoming Intent toBroadcastReceiver.onReceive()
.
要向BroadcastReceiver和IntentFilter注册
系统,获取LocalBroadcastManager的实例并调用其
registerReceiver()
方法.
To register the BroadcastReceiver and the IntentFilter with the
system, get an instance of LocalBroadcastManager and call its
registerReceiver()
method.
LocalBroadcastManager.getInstance(this)
.registerReceiver(mDownloadStateReceiver, statusIntentFilter);
这篇关于在主线程中从Firebase Service访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!