将广播从“服务"发送到“活动"? [英] send broadcast from Service to Activity?

查看:92
本文介绍了将广播从“服务"发送到“活动"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从服务向活动发送广播时遇到问题.

I'm having a problem with sending a broadcast from a Service to an activity.

这是我在服务"课程中所拥有的:

This is what I have in my Service class:

Intent intent = new Intent();
intent.setAction(BROADCAST_ACTION);
sendBroadcast(intent);

我有很多活动,在我的一项活动中,我有这个活动:

I have many Activities and in one of my activities I have this:

   class MyBroadcast extends BroadcastReceiver {
            @Override
            public void onReceive(Context ctxt, Intent i) {


                System.out.println("received");

            }
        };

我的问题是我的广播接收器什么也没收到!

The problem I have is that my broadcast receiver doesn't receive anything!!

帮助!

如果我有很多活动,如何向所有所有人发送广播消息.换句话说,我可以将相同的广播接收器应用于所有活动吗?

If I have many activities how can send a broadcast message to all of them. In other words can I apply the same broadcast receiver to all the activities !?

推荐答案

就像其他人所说的那样,您需要先注册活动才能接收这些广播(请参阅Flo的答案)

Like others said, you need to register the activity first to receive those broadcasts (see Flo's answer)

对于您的其他要求(例如:EDIT).如果您要执行相同的操作,则应创建一个总体活动,并让其他活动扩展该活动.

For your other quesition (re: EDIT). If you are taking the same action, you should create an overall Activity, and have your other activities extend that activity..

然后在此超类中,在onResume上实现广播接收器寄存器,并在onStop上取消注册.

Then in this super class, implement the broadcast receiver registers on onResume and un register onStop..

这篇关于将广播从“服务"发送到“活动"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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