将 String 从 Broadcastreceiver 传递给 MainActivity 并将其放入 Fragment
Pass String from Broadcastreceiver to MainActivity and get it into a Fragment
我必须将一个字符串从 Broadcastreceiver 传递到 MainActivity。然后我会在 Fragment 中得到这个字符串,我该怎么做?
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;
}
}
我尝试了所有方法但没有任何效果:(
有几种方法,根据您的描述,一种选择是:
在 Activity 中声明广播接收器,并将值存储在也在 Activity 中的字符串变量中。
然后,您可以通过片段的构造函数将 activity 中的值传递给片段的构造函数。
我会将广播接收器放在片段中。
这没什么难的。简单的描述方式。
例如:您正在传递数据以从 A 方式广播,
在答:
Intent intent = new Intent(getBaseContext(),
AlarmReceiver.class);
intent.putExtra("alarmTitle", "Alarm at this time!");
startActivity(intent);
在警报接收器中:
// i skipped some lines and mentioned important stuffs alone..
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, NotificationFragment.class);
i.putExtra("PASSDATA","getData");
i.putExtra(Constants.DATAS,
intent.getExtras().getString("alarmTitle"));// this is you are getting from previous activity
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
在 NotificationFragment 中:
Bundle bundle = getIntent().getExtras();
if (bundle != null && bundle.containsKey(Constants.DATAS)&& bundle.containsKey("PASSDATA")) {
String eventStrName = bundle.getString(Constants.DATAS); //
String broadCastData=bundle.getString("PASSDATA");//
//now you got the datas from broadcast receiver
}
希望我涵盖了所有场景。
最简单的方法是使用 Square 的 Otto Library。
http://square.github.io/otto/
创建您的自定义 class
public class InformationEvent {
private String inf;
public InformationEvent(String inf) {
this.inf = inf;
}
public String getInf() {
return inf;
}
}
为 BUS 创建单例
public final class BusProvider {
private static final Bus BUS = new Bus();
public static Bus getInstance() {
return BUS;
}
private BusProvider() {
}
}
在 BroadcastReceiver
中发布您的活动
BusProvider.getInstance().post(new InfromationEvent("your string"));
在您的 Fragment
中订阅此活动
@Subscribe public void answerAvailable(InformationEvent event) {
String yourString = event.getInf();
}
别忘了注册
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
BusProvider.getInstance().register(this);
}
并在您的片段中注销 BUS
@Override
public void onDestroyView() {
super.onDestroyView();
BusProvider.getInstance().unregister(this);
}
我必须将一个字符串从 Broadcastreceiver 传递到 MainActivity。然后我会在 Fragment 中得到这个字符串,我该怎么做?
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;
}
}
我尝试了所有方法但没有任何效果:(
有几种方法,根据您的描述,一种选择是: 在 Activity 中声明广播接收器,并将值存储在也在 Activity 中的字符串变量中。 然后,您可以通过片段的构造函数将 activity 中的值传递给片段的构造函数。 我会将广播接收器放在片段中。
这没什么难的。简单的描述方式。
例如:您正在传递数据以从 A 方式广播,
在答:
Intent intent = new Intent(getBaseContext(),
AlarmReceiver.class);
intent.putExtra("alarmTitle", "Alarm at this time!");
startActivity(intent);
在警报接收器中:
// i skipped some lines and mentioned important stuffs alone..
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, NotificationFragment.class);
i.putExtra("PASSDATA","getData");
i.putExtra(Constants.DATAS,
intent.getExtras().getString("alarmTitle"));// this is you are getting from previous activity
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
在 NotificationFragment 中:
Bundle bundle = getIntent().getExtras();
if (bundle != null && bundle.containsKey(Constants.DATAS)&& bundle.containsKey("PASSDATA")) {
String eventStrName = bundle.getString(Constants.DATAS); //
String broadCastData=bundle.getString("PASSDATA");//
//now you got the datas from broadcast receiver
}
希望我涵盖了所有场景。
最简单的方法是使用 Square 的 Otto Library。 http://square.github.io/otto/
创建您的自定义 class
public class InformationEvent {
private String inf;
public InformationEvent(String inf) {
this.inf = inf;
}
public String getInf() {
return inf;
}
}
为 BUS 创建单例
public final class BusProvider {
private static final Bus BUS = new Bus();
public static Bus getInstance() {
return BUS;
}
private BusProvider() {
}
}
在 BroadcastReceiver
BusProvider.getInstance().post(new InfromationEvent("your string"));
在您的 Fragment
@Subscribe public void answerAvailable(InformationEvent event) {
String yourString = event.getInf();
}
别忘了注册
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
BusProvider.getInstance().register(this);
}
并在您的片段中注销 BUS
@Override
public void onDestroyView() {
super.onDestroyView();
BusProvider.getInstance().unregister(this);
}