如何将数据从广播接收器发送到正在运行的活动, [英] How to send data to a running activity from Broadcast Receiver,

查看:109
本文介绍了如何将数据从广播接收器发送到正在运行的活动,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够接受C2DM消息不错,但我想将数据发送到正在运行的活动,即活动正在运行时,如果接收器接收C2DM消息时,它是将数据发送到正在运行的活动。接收器的code是(在code没有错误):

I am able to receive C2DM message fine but I want to send the data to a running activity, i.e when the activity is running, if the receiver receives C2DM message it is to send the data to the running activity. 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());

我不知道放什么的IntentFilter的()内,也还有什么我有,虽然活动运行将在活动和接收器的code的code和一些C2DM消息到来的接收器可以将数据发送到正在运行的活动。

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.

所以,请告诉我code表示是把在活动和在接收器,也可以是在清单,以便从接收器中的数据可以被发送到运行活性

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.

任何意见是高度AP preciated。

Any advice is highly appreciated.

推荐答案

首先它不是订阅C2DM接收器在活动的最佳想法。做到这一点的体现。为了将数据传递到活动中,您可以在活动创建静态串场并设置串在那里。

First of all it's not the best idea to subscribe c2dm receiver in activity. Do it in manifest. For passing data to activity you can create static string field in Activity and set you String there.

您可以做这样的事情:

活动

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

在你的的BroadcastReceiver

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

这篇关于如何将数据从广播接收器发送到正在运行的活动,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆