将数据从适配器发送到广播接收器并以片段形式接收

时间:2017-09-09 12:13:23

标签: android android-fragments broadcastreceiver

将数据发送到接收方并获取片段

我想接收从Adapter传递的数据,但它甚至没有被调用

接收器类(该课程在Fragement中)

 public class DataReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            if (intent != null) {
                Category category = (Category) intent.getSerializableExtra("category");
                Log.e("data", category.getName());
            }
        } catch (Exception e) {
            Log.e("catch", e.toString());

        }

    }
}

我在下面的片段中创建了接收器,并在这里注册和取消注册接收器。

AddServiceFragment类(接收器在其中)

  @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        bindViews();
        dataReceiver = new DataReceiver();
        filter = new IntentFilter("action");


    }
@Override
    public void onStart() {
        super.onStart();
        getActivity().registerReceiver(dataReceiver, filter);
    }


    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        getActivity().unregisterReceiver(dataReceiver);
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        getActivity().registerReceiver(dataReceiver, filter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(dataReceiver);

    }

我使用下面的代码将数据发送到接收器

适配器代码

 holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent in = new Intent(context, AddServiceFragment.DataReceiver.class);
            in.setAction("action");
            in.putExtra("category", category);
            context.sendBroadcast(in);
        }
    });

2 个答案:

答案 0 :(得分:0)

你使用错误的方式。请查看下面的示例代码。

片段类中的代码:

@Override
    public void onResume() {
        getActivity().registerReceiver(mReceiverLocation, new IntentFilter("data_action"));
        super.onResume();
    }

    @Override
    public void onPause() {
        getActivity().unregisterReceiver(mReceiverLocation);
        super.onPause();
    }

    private BroadcastReceiver mReceiverLocation = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                Category category = (Category) intent.getSerializableExtra("category");
                Log.e("data", category.getName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

从适配器类调用:

holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent in = new Intent("data_action");
                in.putExtra("category", category);
                context.sendBroadcast(in);
            }
        });

答案 1 :(得分:0)

看看这个: https://developer.android.com/training/basics/fragments/communicating.html 您可以通过主机活动在您的片段之间进行通信。

  

通常,您会希望一个片段与另一个片段进行通信,例如根据用户事件更改内容。所有Fragment-to-Fragment通信都是通过相关的Activity完成的。两个碎片永远不应该直接沟通。