2015-03-25 57 views
1

我必须将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; 
    } 
} 

我尝试了一切,但没有任何工作:(

回答

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

好吧,我用你的代码,我把吐司在answerAvailable但是当广播接收器被称为I无法看到Toast通知.. – Slaiv206 2015-03-25 11:29:50

+0

@ Slaiv206确保您在'Activity'和'BroadcastReceiver'中使用相同的Bus实例。 – 2015-03-25 12:13:56

+0

@EgorN:我发布了代码,我不明白你的意思,请你解释一下更好吗? – Slaiv206 2015-03-25 12:26:16

0

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

-1

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

对于例如:要传递数据从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 
} 

希望我介绍了所有情况。

相关问题