zoukankan      html  css  js  c++  java
  • Android Fragment间的广播消息接收

    这种方式不用在配置文件加东西,我比较喜欢。

    广播注册,可以写在Activity(onCreate),也可以写在Fragment(onActivityCreated)里。

    LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getActivity());
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("android.intent.action.CART_BROADCAST");//建议把它写一个公共的变量,这里方便阅读就不写了。
    BroadcastReceiver mItemViewListClickReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent){
                    System.out.println("OK");
                }
     };
     broadcastManager.registerReceiver(mItemViewListClickReceiver, intentFilter);

    发送广播

    Intent intent = new Intent("android.intent.action.CART_BROADCAST");
    LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);
  • 相关阅读:
    Java
    Java
    Java
    其他
    Java
    Java
    Java
    正则
    Win10
    【转】Flask 上下文机制和线程隔离
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/7779240.html
Copyright © 2011-2022 走看看