标签:
LocalBroadcastManager是Android Support包提供了一个工具,是用来在同一个应用内的不同组件间发送Broadcast的。
使用LocalBroadcastManager有如下好处:
发送广播:
final Intent intent = new Intent(UartService.DATAUPDATA);
LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);
接收广播:
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
updateReceiver, makeGattUpdateIntentFilter());
private static IntentFilter makeGattUpdateIntentFilter() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(UartService.DATAUPDATA);
return intentFilter;
}
private final BroadcastReceiver updateReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
ToastUtil.toast(getActivity(), action);
}
};
标签:
原文地址:http://www.cnblogs.com/zhaoleigege/p/5497361.html