将Broadcastreceiver中的字符串传递给MainActivity并将其转换为片段

时间:2015-03-25 10:39:20

标签: android android-fragments android-broadcast

我必须将一个String从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;
    }
}

我尝试了一切,但没有任何效果:(

3 个答案:

答案 0 :(得分:4)

最简单的方法是使用Square的Otto Library。 http://square.github.io/otto/

创建自定义类

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);
}

答案 1 :(得分:0)

有几种方法,根据您的描述,一个选项是: 在Activity中声明Broadcast接收器,并将值存储在Activity中的字符串变量中。 然后,您可以通过片段的构造函数将活动中的值传递给构造函数。 我会将广播接收器放在片段中。

答案 2 :(得分:-1)

没什么难的。简单的描述方式。

例如:您正在通过A手段将数据传递给广播,

在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
}

希望我涵盖所有情景。