广播接收器在某些情况下不工作 [英] Broadcast receiver not working in some cases

查看:181
本文介绍了广播接收器在某些情况下不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个接收器在我Fragment.class

 私人广播接收器接收器=新的广播接收器(){
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        如果(intent.getAction()等于(WeekplanHepler.ACTION_SERVICE_BINDED)及&放大器; getUpdateService()!= NULL){
            getCounts();
        }        如果(intent.getAction()。等于(Helper.UpdateCounts)){
            HashMap的<字符串,整数>数=(HashMap的<字符串,整数>)intent.getSerializableExtra(Helper.PARAM_LIST_COUNTS);
            //更新计数
        }
    }
};

有关注册/ uregistering该接收器我用这code:

  @覆盖
公共无效调用onStart(){
    super.onStart();
    getCounts(真);
    。getActivity()registerReceiver(接收机,getIntentFilter());
}@覆盖
公共无效的onStop(){
    super.onStop();
    。getActivity()unregisterReceiver(接收机);
}

getCounts()是UpdateService puted一个RetrofitRequest,这是getUpdateService()这里

所以,当要求改造已经完成,它通过意向返回计数,然后,你看,我更新它们。但是,如果我去下一个活动,然后返回,请求将做工精细,并且它会发出意图,但不会接收得到它。我认为这可以通过方法的服务引起被排在首位绑定。所以我有BaseFragment,这对于需要

所有片段结合服务

 公共抽象类BaseFragment< T>扩展CustomListFragment< T> {
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        getActivity()。getApplicationContext()。bindService(新意图(getActivity()。getApplicationContext()
                WeekplanUpdateService.class),连接Context.BIND_AUTO_CREATE);
    }
}

当我去到下一个活动,它的片段将绑定的服务,我的previous片段将再次调用请求,将有计数。也许接收机有他能得到多少相同的意图一定的局限性,但我不这么认为。请帮助

哦,我忘了,更不用说如何我发送意图

  sendBroadcast(新意图(Helper.UpdateCounts).putExtra(Helper.PARAM_LIST_COUNTS,计数));


解决方案

所以,当我开始使用

  sendStickyBroadcast(新意图(Helper.UpdateCounts)
                    .putExtra(Helper.PARAM_LIST_COUNTS,计数));

而不是

  sendBroadcast(新意图(Helper.UpdateCounts)
                    .putExtra(Helper.PARAM_LIST_COUNTS,计数));

它的工作。

So i have this receiver in my Fragment.class

private BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(WeekplanHepler.ACTION_SERVICE_BINDED) && getUpdateService() != null) {
            getCounts();
        }

        if (intent.getAction().equals(Helper.UpdateCounts)) {
            HashMap<String, Integer> counts = (HashMap<String, Integer>) intent.getSerializableExtra(Helper.PARAM_LIST_COUNTS);
            // Updating counts
        }
    }
};

For registering/uregistering this receiver i'm using this code:

@Override
public void onStart() {
    super.onStart();
    getCounts(true);
    getActivity().registerReceiver(receiver, getIntentFilter());
}

@Override
public void onStop() {
    super.onStop();
    getActivity().unregisterReceiver(receiver);
}

getCounts() is a RetrofitRequest puted in UpdateService, which is getUpdateService() here

So, when retrofit request has been finished, it returns counts through Intent and then, as you see, i'm updating them. But if i go to next Activity and then returns, request will work fine, and it will send intent, but receiver wont get it. I think this can be caused by method service is binded in first place. So i have BaseFragment, which binds service for all Fragments needed

public abstract class BaseFragment<T> extends CustomListFragment<T> {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().getApplicationContext().bindService(new Intent(getActivity().getApplicationContext(), 
                WeekplanUpdateService.class), connection, Context.BIND_AUTO_CREATE);
    }
}

When i go to next activity, it fragment will bind service, and my previous fragment will call request again, and will have counts. Maybe receiver has some limitations for how much he can get same intents, but i don't think so. Please help

Oh, i forgot to mention how i'm sending intent

sendBroadcast(new Intent(Helper.UpdateCounts).putExtra(Helper.PARAM_LIST_COUNTS, counts));

解决方案

So, when i've started to use

sendStickyBroadcast(new Intent(Helper.UpdateCounts)
                    .putExtra(Helper.PARAM_LIST_COUNTS, counts));

instead of

sendBroadcast(new Intent(Helper.UpdateCounts)
                    .putExtra(Helper.PARAM_LIST_COUNTS, counts));

it worked.

这篇关于广播接收器在某些情况下不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆