java - 广播接收器 onReceive 在 fragment 中不起作用

标签 java android

我将 broadcastreceiver 从 Activity 发送到 fragment 。我的问题是 onReceive 不工作。

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

    IntentFilter(Constants.BroadCastMessages.UI_NEW_CHAT_ITEM));

    onNotice = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action){

                case Constants.BroadCastMessages.UI_NEW_CHAT_ITEM:

                    Log.d("RokayahBroadcast" , "from on receive");
            }

        }
    };

  public void onResume() {
    super.onResume();

    IntentFilter iff = new 
       IntentFilter(Constants.BroadCastMessages.UI_NEW_CHAT_ITEM);
    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(onNotice , iff);

   @Override
public void onPause() {
    super.onPause();
    getActivity().unregisterReceiver(onNotice);

请帮忙。 提前致谢

最佳答案

据我所知,BroadcastReceiver 不能注册到 Fragment 中,而是注册到 Activity 中:您可能应该在 Activity 级别注册此接收器,然后在收到后将其传递给您的 Fragment。

关于java - 广播接收器 onReceive 在 fragment 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53847703/

相关文章:

java - 我收到 BeanFactory 错误 : NoSuchBeanDefinitionException and I can't figure out how to solve it

Java增量问题

android - 在 Android 中播放音频 RTMP 流的最简单方法

java - 安卓相机: Unboxing of 'characteristics.get(CameraCharacteristics.LENS_FACING)' may produce NPE

android - 如何在自定义适配器内生成的按钮中设置一些 Action

javascript - Jquery 遍历输入字段的祖先

java - 银行如何通过允许一次登录来维持 session

java - 包 Javax.portlet 不存在

android - ClassCastException : NoClassDefFoundError cannot be cast to RuntimeException

android - Android Studio 自动更改每个项目的背景