簡體   English   中英

如何從BroadcastReceiver更新片段選項卡?

[英]How to update fragment tab from BroadcastReceiver?

當我的程序中的某個標志被設置時,我發送意圖,然后在BroadcastReceiver中接收此意圖。 我不知道如何從這里更新選項卡片段。 有什么建議或例子嗎?

設置標志時我得到這個日志,但是我不知道如何從那里開始:

public class MyReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
      Toast.makeText(context, "hello :)", Toast.LENGTH_LONG).show();
   }  
}

感謝幫助!

分段:

public class FragmentInfo extends Fragment {

private TextView textView1;
    private TextView textView3;
    private TextView textView5;
    private TextView textView7;
    private TextView textView8;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View myFragmentView = inflater.inflate(R.layout.fragment_info, container, false);

    connectButton = (Button) myFragmentView.findViewById(R.id.button1);
    changeSettingsButton = (Button) myFragmentView.findViewById(R.id.button2);
    erraseFlightsButton = (Button) myFragmentView.findViewById(R.id.button3);

    //TextView for status of connected device..
    textView1 = (TextView)myFragmentView.findViewById(R.id.TextView1);
    //TextView for Device name
    textView3 = (TextView)myFragmentView.findViewById(R.id.TextView3);
    //TextView for Serial number
    textView5 = (TextView)myFragmentView.findViewById(R.id.TextView5);
    //TextView for Software version
    textView7 = (TextView)myFragmentView.findViewById(R.id.TextView7);
    //TextView for Connected Device version
    textView8 = (TextView)myFragmentView.findViewById(R.id.TextView8);  


    return myFragmentView;
}

}
  1. 使Receiver類成為控制選項卡的嵌套類。 這樣您就可以訪問通過片段方法刷新數據的方法。 此接收器應分別在onStart()onPause()注冊和取消注冊(將這些接收器包裝在try-catch塊中,因為某些版本的Android可能會在注冊或取消注冊時崩潰)。 此接收器應該是控制片段的活動類的嵌套類。 不要把它放在片段類本身。

  2. 使其成為頂級類,並通過setUpdateListener(YourListener)等方法傳遞偵聽器。 在控制選項卡的活動中實現偵聽器。

  3. 還有一個Messenger類,你可以通過它傳遞內部進程。

編輯:無論哪種方式與更新片段的接收器,不要在清單中注冊,而是通過代碼。 沒有接收器,更新UI,在沒有UI顯示時觸發。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM