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

发布于 2024-12-22 04:28:39 字数 945 浏览 0 评论 0原文

我能够很好地接收 C2DM 消息,但我想将数据发送到正在运行的活动,即当活动正在运行时,如果接收者收到 C2DM 消息,则会将数据发送到正在运行的活动。接收器的代码是(代码中没有错误):

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 );
       }
     }}

我在活动中尝试过这样的操作,试图在活动中注册接收器,以便接收器可以发送数据并且正在运行的活动可以接收数据:-

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

我不知道不知道要在 IntentFilter() 中放入什么,也不知道我还必须在活动代码和接收器代码中放入什么,以便在活动运行且出现一些 C2DM 消息时接收器可以将数据发送到跑步活动。

因此,请告诉我要放入活动和接收器中的代码,也可能放在清单中,以便将来自接收器的数据发送到正在运行的活动。

任何建议都将受到高度赞赏。

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());

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一直在等你来 2024-12-29 04:28:39

首先,在活动中订阅 c2dm 接收器并不是最好的主意。在清单中执行此操作。为了将数据传递到活动,您可以在活动中创建静态字符串字段并在那里设置字符串。

您可以执行以下操作:

Activity 中,

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 {
...
}

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.

You can do something like this:

in 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:

@Override
public void onReceive(Context context, Intent intent) {
...
if (YourActivity.mThis != null) {
    ((TextView)YourActivity.mThis.findViewById(R.id.text)).setText("received c2dm");
}
else {
...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文