java - 广播接收器 onReceive 在片段中不起作用
问题描述
我将广播接收器从活动发送到片段。我的问题是 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。
推荐阅读
- angular - 如何在打字稿中提交表单(发布方法)后立即从服务器获取生成的ID
- wordpress - Cloudfront 似乎并没有提高网站的性能
- ansible - Azure Pipelines 上的 Ansible yaml 文件报告错误
- java - Java currentTimeMillis() 使用了多次问题
- java - 可能接口内部继承了对象类
- c# - GetComboBoxInfo 的 64 位兼容性
- react-native - SafeAreaView 中的动态背景颜色反应原生
- javascript - 在 Javascript 中创建文件并触发下载
- javascript - 未捕获(承诺中):错误:IE11 中的访问被拒绝
- javascript - 取消选中单选按钮时,电子邮件字段消失