安卓-广播LocalBroadcastManager
温馨提示:
要保证发送消息和接受消息使用的是同一个Activity。
发送消息
Intent intent = new Intent(BROADCAST_RECEIVER);
intent.putExtra("x", 1);
intent.putExtra("y", 1);
LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);
接受消息
//注册
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mBroadcastReceiver, new IntentFilter(BROADCAST_RECEIVER));
mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// 获取点击位置
float x = intent.getFloatExtra("x", -1);
float y = intent.getFloatExtra("y", -1);
Rect fragmentBounds = new Rect();
rootView.getGlobalVisibleRect(fragmentBounds);
if(fragmentBounds.contains((int)x, (int)y)){
LOGGER.info("在范围内");
}else{
LOGGER.info("在范围外");
}
}
};
//LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mBroadcastReceiver);