android - 如何在 BroadCastReceiver 的 onReceive 方法中的 TextView 中设置文本?

标签 android

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

为什么(context.findviewById() 不起作用),因为 findViewById() 是 Activity 类的一部分,而 context.toString() 是否显示 Activity( base activity ) 类的引用?

最佳答案

以下答案的链接在这里:Communicating with Other Fragments

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

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

公共(public)类 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");
    }
}

...

} 现在, fragment 可以通过使用 OnHeadlineSelectedListener 接口(interface)的 mCallback 实例调用 onArticleSelected() 方法(或接口(interface)中的其他方法)来向 Activity 传递消息。

例如,当用户点击列表项时调用 fragment 中的以下方法。该 fragment 使用回调接口(interface)将事件传递给父 Activity 。

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

实现接口(interface) 为了从 fragment 接收​​事件回调,承载 fragment 的 Activity 必须实现 fragment 类中定义的接口(interface)。

例如,以下 Activity 实现了上述示例中的接口(interface)。

公共(public)静态类 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 的公共(public)方法。

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

公共(public)静态类 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();
    }
}

关于android - 如何在 BroadCastReceiver 的 onReceive 方法中的 TextView 中设置文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15496926/

相关文章:

android - 数据绑定(bind)布局奇怪的构建错误

android - 不使用 --min-sdk-version >= 24 的默​​认或静态接口(interface)方法

android - 在向上导航中的 Activity A 上调用 onCreate

java - 后台服务的延迟通知

java - 在 Glide 4 中使用 AppGlideModule 中的 RequestOptions

android - 如何获取收发邮件信息?

java - 将 JDBC 导入 Android Studio 错误

java - 来自不同类的自定义 SeekBarPreference setProgress?

android - 通过 Maven 多次部署到 USB 集线器上的 Android 设备

android - 频闪相机参数?为什么我的应用程序崩溃了?