Fragment里面获取广播(转)

研究了一上午的在Fragment获取广播,然后在网上查了一些资料,结果还是没有实现接受到广播,但最后终于看到一篇帖子是可以实现的,就转载过来了。
内容如下:
在开发中有时候会遇见一些的情况:根据不同的需求切换不同的fragment ,然后一些操作使当前fragment中显示的内容进行一些调整。很多时候我们都是想的在fragment添加到Activity中时通过 fragmet. getView()获取fragment对象的View 。然后用findViewById()获取到fragment中的控件对象进行操作,很悲剧的是不管用啊有木有。我是个小白,研究了近一周都没搞定,只有采用另一种方式对fragment中的控件进行操作了。那就是广播。    UI中需要时发送一条广播 可顺便传入一些需要的值,在fragment中接收广播,获取值并对fragment中的控件进行操作,经本人验证完全可以。以下是Demo关键代码
在Fragment中
[java]   view plain copy

  1. public class GasFragment extends Fragment {  
  2.     private TextView gasName;  
  3.     private TextView gasadderss;  
  4.       
  5.     private ReceiveBroadCast receiveBroadCast;  
  6.     public  GasFragment (){}  
  7.       
  8.     @Override  
  9.     public void onAttach(Activity activity) {  
  10.          
  11.         /** 注册广播 */  
  12.         receiveBroadCast = new ReceiveBroadCast();  
  13.         IntentFilter filter = new IntentFilter();  
  14.         filter.addAction("com.gasFragment");    //只有持有相同的action的接受者才能接收此广播  
  15.         activity.registerReceiver(receiveBroadCast, filter);  
  16.         super.onAttach(activity);  
  17.     }  
  18.     @Override  
  19.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  20.             Bundle savedInstanceState) {  
  21.             View view=inflater.inflate(R.layout.fragment_gas, null);  
  22.   
  23.         gasName = (TextView) view.findViewById(R.id.frag_tv_gasname);  
  24.         gasadderss = (TextView) view.findViewById(R.id.fraggas_tv_adderss);  
  25.          
  26.          
  27.             }   
  28.       });  
  29.         return view;  
  30.     }  
  31.      class ReceiveBroadCast extends BroadcastReceiver  
  32.         {  
  33.                 @Override  
  34.                 public void onReceive(Context context, Intent intent)  
  35.                 {  
  36.                     //得到广播中得到的数据,并显示出来  
  37.                     String gasname = intent.getExtras().getString("gasName");  
  38.                     String address = intent.getExtras().getString("address");  
  39.                      
  40.                     gasadderss.setText("地址:\n  "+address);  
  41.                     gasName.setText(gasname);  
  42.                 }  
  43.         }  
  44.      /**
  45.       *注销广播
  46.       * */  
  47.      @Override  
  48.      public void onDestroyView() {  
  49.         getActivity().unregisterReceiver(receiveBroadCast);  
  50.         super.onDestroyView();  
  51.      }  
  52.       
  53.      }     
复制代码



在UI中 [java]   view plain copy

  1. public class MainPage extends FragmentActivity {  
  2.   
  3.     private GasFragment gasFragment;       //加油站的fragment  
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.          
  8.         setContentView(R.layout.main_page);  
  9.         gasFragment = new GasFragment();  
  10.         FragmentManager fm = getSupportFragmentManager();  
  11.         FragmentTransaction ft = fm.beginTransaction();  
  12.         ft.replace(R.id.main_fl_fragment, couponFragment);// 将帧布局替换成Fragment  
  13.         ft.commit();// 提交  
  14.          
  15.         Intent intent = new Intent(); // Itent就是我们要发送的内容  
  16.         intent.setAction("com.gasFragment"); // 设置你这个广播的action  
  17.         intent.putExtra("gasName","核反应能量加油站");  
  18.         intent.putExtra("address", "太平洋街11号");  
  19.         sendBroadcast(intent); // 发送广播  
  20.     }  
  21. }  
复制代码

猜你喜欢

转载自blog.csdn.net/huluobobb/article/details/51384019