2015-06-07 51 views

回答

0

在ListFragment創建和註冊BroadcastReceiver

static final String ACTION_CUSTOM = "action"; 
BroadcastReceiver mReceiver; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mReceiver = new BroadcastReceiver(){ 
     public void onReceive(android.content.Context context, Intent intent) { 

      //Check for action you need 
      if (intent.getAction() == ACTION_CUSTOM){ 
       //Do stuff with data from intent 
       intent.getExtras().getString("data"); 
      } 
     } 
    }; 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    getActivity().registerReceiver(receiver, new IntentFilter(ACTION_CUSTOM)); 
} 


@Override 
protected void onPause() { 
    super.onPause(); 
    getActivity().unregisterReceiver(receiver); 
} 

在MapFragment發送廣播意圖,當您需要更改數據:

Intent intent = new Intent(); 

//Set action you need to send 
intent.setAction(ACTION_CUSTOM); 

//Add data to send 
intent.putExtra("data", "data_value"); 

//Send broadcast intent 
getActivity().sendBroadcast(intent); 
+0

嗯。謝謝,但它沒有回答我的問題。我想知道如何做到這一點//對你的數據做些什麼,例如如何refredh listFragment – Traabefi

相關問題