2015-03-25 83 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 
} 

希望我介紹了所有情況。

相關問題