我必须将Broadcast从BroadcastAcerer传递给MainActivity。然后,我会将这个字符串放在碎片中,我该如何做到这一点?将字符串从BroadcastReceiver传递给MainActivity,并将其获取到片段中
MyReceiver.java
public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Bus bus = new Bus();
bus.post(new InformationEvent("your string"));
}
}
ScarsdaleHome.java(片段)
@Subscribe public void answerAvailable(InformationEvent event) {
String yourString= event.getInf();
Toast.makeText(getActivity(), yourString, Toast.LENGTH_LONG).show();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.scarsdale_home_activity, container, false);
bus = new Bus();
bus.register(this);
return rootView;
}
InformationEvent.java
public class InformationEvent {
private String inf;
public InformationEvent(String inf) {
this.inf = inf;
}
public String getInf() {
return inf;
}
}
我尝试了一切,但没有任何工作:(
好吧,我用你的代码,我把吐司在answerAvailable但是当广播接收器被称为I无法看到Toast通知.. – Slaiv206 2015-03-25 11:29:50
@ Slaiv206确保您在'Activity'和'BroadcastReceiver'中使用相同的Bus实例。 – 2015-03-25 12:13:56
@EgorN:我发布了代码,我不明白你的意思,请你解释一下更好吗? – Slaiv206 2015-03-25 12:26:16