如何从广播接收器将数据发送到正在运行的活动,
我能够很好地接收 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,在活动中订阅 c2dm 接收器并不是最好的主意。在清单中执行此操作。为了将数据传递到活动,您可以在活动中创建静态字符串字段并在那里设置字符串。
您可以执行以下操作:
在
Activity
中,在您的
BroadcastReceiver
中: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
In your
BroadcastReceiver
: