我正在使用gcm从我的服务器获取事件,这很好但我想使用我的类GCMIntentService与其他人交流
我有接收消息的方法
@Override
protected void onMessage(Context context, Intent intent) {
Log.d("GCM", "RECIEVED A MESSAGE");
// Get the data from intent and send to notificaion bar
generateNotification(context, intent);
}
我想例如将数据发送到我的MainActivity,但我找不到如何做到这一点 我找到了:
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);
但在我的情况下,我不想开始新活动,我只想将数据发送到正在运行的活动。
感谢您的帮助
答案 0 :(得分:1)
我找到了解决方案,@ selalerer感谢您的帮助
第一次启动接收器:
//receive events
private class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
System.out.println("receive");
}
}
onCreate add 中的
IntentFilter filter = new IntentFilter("local");
this.registerReceiver(new Receiver(), filter);
完成使用
发送消息Intent i=new Intent();
i.setAction("local");
i.putExtra("test","test");
sendBroadcast(i);