简体   繁体   English

如何从广播接收器向正在运行的活动发送数据,

[英]How to send data to a running activity from Broadcast Receiver,

I am able to receive C2DM message fine but I want to send the data to a running activity, ie when the activity is running, if the receiver receives C2DM message it is to send the data to the running activity. 我能够很好地接收C2DM消息,但我想将数据发送到正在运行的活动,即当活动正在运行时,如果接收方收到C2DM消息,则将数据发送到正在运行的活动。 The code of receiver is (no bugs in the code): 接收者的代码是(代码中没有错误):

public class C2dmreceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.w("C2DM", "Message Receiver called");
        if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) 
        {
            final String payload = intent.getStringExtra("key1");   
            Log.d("C2DM", "message = " + payload );
       }
     }}

I have tried like this inside the activity in an attempt to register the receiver in the activity so that the receiver can send data and the running activity can receive the data :- 我尝试在活动中试图在活动中注册接收器,以便接收器可以发送数据,并且运行活动可以接收数据:

C2dmreceiver c2dmr = new C2dmreceiver();
Registration.this.registerReceiver(c2dmr, new IntentFilter());

I don't know what to put inside the IntentFilter(), also what else I have to put in the code of the activity and the code of the receiver so that while the activity is running and some C2DM message comes the receiver can send the data to the running activity. 我不知道在IntentFilter()中放什么,还有什么我必须放在活动的代码和接收器的代码中,这样当活动运行时,一些C2DM消息来了接收器可以发送数据到正在运行的活动。

So, please tell me the code that is to put in the activity and in the receiver and may also be in the manifest so that the data from the receiver could be send to running activity. 因此,请告诉我要放入活动和接收器的代码,也可以在清单中,以便来自接收器的数据可以发送到正在运行的活动。

Any advice is highly appreciated. 任何建议都非常感谢。

First of all it's not the best idea to subscribe c2dm receiver in activity. 首先,在活动中订阅c2dm接收器并不是最好的主意。 Do it in manifest. 在清单中做它。 For passing data to activity you can create static string field in Activity and set you String there. 要将数据传递给活动,您可以在Activity中创建静态字符串字段,并在那里设置String。

You can do something like this: 你可以这样做:

in Activity Activity

public static YourActivity mThis = null;
@Override
protected void onResume() {
    super.onResume();
    mThis = this;
}
@Override
protected void onPause() {
    super.onPause();
    mThis = null;
}

In your BroadcastReceiver : 在你的BroadcastReceiver

@Override
public void onReceive(Context context, Intent intent) {
...
if (YourActivity.mThis != null) {
    ((TextView)YourActivity.mThis.findViewById(R.id.text)).setText("received c2dm");
}
else {
...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从广播接收器向Android中的活动发送数据 - how to send data from broadcast receiver to activity in android 如何将结果数据从广播接收器发送到活动 - How can I send result data from Broadcast Receiver to Activity 将数据从广播接收器发送到正在运行的服务 - Send data from Broadcast Receiver to running Service 如何将数据从广播接收器发送到片段? - How to send a data from a broadcast receiver to fragment? 如何将数据从活动中的功能传递到通知接收器广播接收器? - How to pass data from function in activity to a Notifcation Receiver Broadcast Receiver? 从活动向广播接收器发送数据时,应用在startActivity时崩溃 - When send data from activity to broadcast receiver, app crashes on startActivity 如何将值从广播接收器发送到“已经开始”活动 - How to send the value from broadcast receiver to Already started Activity 如何使用广播接收器将事件从活动发送到Android中的服务 - how to send events from activity to service in android with broadcast receiver 如何通过广播接收器通知正在运行的活动? - how can I notify a running activity from a broadcast receiver? 从广播接收器向活动发送警报 - Send an alert to an Activity from Broadcast Receiver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM