这种方式不用在配置文件加东西,我比较喜欢。
广播注册,可以写在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);
发送广播
<pre name="code" class="html">ntent intent = new Intent("android.intent.action.CART_BROADCAST");
LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);
原地址:<a target=_blank href="http://blog.sina.com.cn/s/blog_6368ea770101l6yd.html">http://blog.sina.com.cn/s/blog_6368ea770101l6yd.html</a>