Java 如何从BroadcastReceiver更新片段选项卡?

Java 如何从BroadcastReceiver更新片段选项卡?,java,android,Java,Android,当程序中的某个标志被设置时,我发送意图,然后在BroadcastReceiver中接收该意图。我不知道如何从这里更新标签片段。有什么建议或例子吗 当设置标志时,我会得到这个日志,但我不知道如何从那里开始: public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(co

当程序中的某个标志被设置时,我发送意图,然后在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;
}

}
  • 使Receiver类成为控制选项卡的嵌套类。这样,您应该可以访问通过fragment的方法刷新数据的方法。此接收器应分别在
    onStart()
    onPause()
    中注册和注销(将其包装在try-catch块中,因为某些版本的Android可能在注册或注销时崩溃)。此接收器应该是控制片段的activity类的嵌套类。不要把它放在fragment类本身中

  • 使其成为顶级类,并通过类似
    setUpdateListener(YourListener)
    的方法传递侦听器。在控制选项卡的活动中实现侦听器

  • 还有
    Messenger
    类,您可以将其传递给进程内部通信


  • 编辑:无论哪种方式,对于更新片段的接收者,都不在清单中注册,而是通过代码注册。不需要一个接收器,它可以更新用户界面,在没有显示用户界面时触发。

    请添加标签碎片的代码我添加了一些碎片的基本代码。这对我来说太多了。我在片段中创建了那个类,但现在出现了错误:java.lang.RuntimeException:无法实例化receiver com.example.device.MyReceiver:java.lang.ClassNotFoundException:在那里可能看不到类…我已经更新了答案。另外,应该使用
    $
    符号-
    com.example访问xml文件中的任何嵌套类。device$MyReceiver
    给定
    device
    是顶级类。