1

基本上我想在 BroadCaseReceiver 中执行 TextView 类的 setText() 功能。但我无法实现这一点:我不知道

为什么(context.findviewById()不起作用),因为findViewById()它是 Activity 类的一部分并且context.toString()显示了 Activity(基本活动)类的引用?

4

3 回答 3

2

以下答案的链接在这里:与其他片段通信

要允许 Fragment 与其 Activity 进行通信,您可以在 Fragment 类中定义一个接口并在 Activity 中实现它。Fragment 在其 onAttach() 生命周期方法期间捕获接口实现,然后可以调用接口方法以与 Activity 通信。

以下是 Fragment 到 Activity 通信的示例:

公共类 HeadlinesFragment 扩展 ListFragment { OnHeadlineSelectedListener mCallback;

// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
    public void onArticleSelected(int position);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        mCallback = (OnHeadlineSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnHeadlineSelectedListener");
    }
}

...

现在片段可以通过使用 OnHeadlineSelectedListener 接口的 mCallback 实例调用 onArticleSelected() 方法(或接口中的其他方法)将消息传递给 Activity。

例如,当用户单击列表项时,将调用片段中的以下方法。片段使用回调接口将事件传递给父活动。

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // Send the event to the host activity
    mCallback.onArticleSelected(position);
}

实现接口 为了从片段接收​​事件回调,承载它的活动必须实现片段类中定义的接口。

例如,以下活动实现了上述示例中的接口。

公共静态类 MainActivity 扩展 Activity 实现 HeadlinesFragment.OnHeadlineSelectedListener{ ...

public void onArticleSelected(int position) {
    // The user selected the headline of an article from the HeadlinesFragment
    // Do something here to display that article
}

向 Fragment 传递消息宿主 Activity 可以通过使用 findFragmentById() 捕获 Fragment 实例来向 Fragment 传递消息,然后直接调用 Fragment 的公共方法。

例如,假设上面显示的活动可能包含另一个片段,用于显示上述回调方法中返回的数据指定的项目。在这种情况下,活动可以将回调方法中接收到的信息传递给将显示该项目的另一个片段:

公共静态类 MainActivity 扩展 Activity 实现 HeadlinesFragment.OnHeadlineSelectedListener{ ...

public void onArticleSelected(int position) {
    // The user selected the headline of an article from the HeadlinesFragment
    // Do something here to display that article

    ArticleFragment articleFrag = (ArticleFragment)
            getSupportFragmentManager().findFragmentById(R.id.article_fragment);

    if (articleFrag != null) {
        // If article frag is available, we're in two-pane layout...

        // Call a method in the ArticleFragment to update its content
        articleFrag.updateArticleView(position);
    } else {
        // Otherwise, we're in the one-pane layout and must swap frags...

        // Create fragment and give it an argument for the selected article
        ArticleFragment newFragment = new ArticleFragment();
        Bundle args = new Bundle();
        args.putInt(ArticleFragment.ARG_POSITION, position);
        newFragment.setArguments(args);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();
    }
}

}

于 2013-03-19T10:58:43.777 回答
0

除非 BroadcastReceiver 在 Activity 的上下文中以编程方式启动,例如 Activity.onCreate()。对于一个普通的 BroadcastReceiver,从它访问一个像 TextView 这样的 UI 元素是很奇怪的。您只能使用 Activity 中的 UI 内容。也许您应该查看您尝试应用的解决方案。即使你设法做这样的事情,它也会有错误的设计。

于 2013-03-19T10:32:08.327 回答
0
public class MyReceiver extends BroadcastReceiver {

private MyActivity myActivity;

public MyReceiver(MyActivity myActivity) {
    this.myActivity= myActivity;
}

@Override
public void onReceive(Context context, Intent intent) {

    Log.d("", "Onrecieve ready to call");
            if(this.myActivity!=null)
            {
                this.myActivity.update();
            }
            // make update method is in your activity.
            // call function of your activity and change UI.
}
}
于 2013-03-19T10:36:42.627 回答