1. 程式人生 > >android fragment間的廣播訊息接收

android fragment間的廣播訊息接收

fragment傳送廣播

        Intent intent = new Intent(MainActivity.ACTION_ITEMVIEW_LISTCLICK);
        intent.putExtra("position", position);
        LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);
主介面註冊接收廣播
 private void initBroadcastListener() {
      mBroadcastManager = LocalBroadcastManager
.getInstance(this);
IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ACTION_ITEMVIEW_LISTCLICK); mItemViewListClickReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(ACTION_ITEMVIEW_LISTCLICK)) { Log.v(TAG, ACTION_ITEMVIEW_LISTCLICK + "," + intent.getIntExtra("position", -1)); } } }; mBroadcastManager.registerReceiver(mItemViewListClickReceiver, intentFilter
);
}