简体   繁体   English

BroadcastReceiver停止工作

[英]BroadcastReceiver stops working

In general i have a service that sends an intent to my activity which is ALWAYS on every 6 sec and a BroadcastReceiver everytime on receive updates a timer. 总的来说,我有一项服务,可以每6秒发送一次意图到我的活动,每次接收时都使用BroadcastReceiver来更新计时器。

I found by accident that after a while ( this is random ) that the particular receiver stops working. 我偶然发现,一段时间后(这是随机的),特定的接收器停止工作。

OnPause i unregister it and onResume i register it again. OnPauseunregister它, onResume我再次注册它。

Also this happens randomly in any devices and android versions. 此外,这在任何设备和android版本中都是随机发生的。

I found by researching on the web , that after onReceive the receiver is ready to killed by Android but mine keeps getting intents. 我在网上进行调查后发现,在onReceive之后,接收器已准备好被Android杀死,但我的意图不断增强。

"Receiver Lifecycle “接收机生命周期

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). BroadcastReceiver对象仅在对onReceive(Context,Intent)的调用期间有效。 Once your code returns from this function, the system considers the object to be finished and no longer active. 一旦您的代码从该函数返回,系统将认为该对象已完成并且不再处于活动状态。 "

FYI i have declare it like this inside my activity 仅供参考,我在活动中这样声明

public class MyReceiver extends BroadcastReceiver {

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

        Log.i("Intent received", "+_ " + intent.getAction());
        if (intent.getAction().equals(TEST)) {
        //do sth
        } else {

        //do sth else
        }


    }
}

Thans a lot even for taking the time to read :). 即使花一些时间阅读:)也很感谢。

I dont declare anythin to my manifest and as for my logcat i must be over my phone the moment it happens. 我没有在清单中声明任何内容,至于我的日志猫,一旦发生,我必须通过电话。 The service is a simple send broadcast after one async task. 该服务是一项异步任务之后的简单发送广播。 The last test i made was ensuring that the code from the service was running by logging the beeing sent. 我进行的最后一个测试是通过记录发送的蜜蜂来确保服务的代码正在运行。 And the service kept on. 和服务继续。

I am away from my code write now but i think there will be no help because is very simple. 我现在不编写代码,但是我认为这将是毫无帮助的,因为它非常简单。 Thnaks 恶作剧

Well i could still figure out but I find a solution to my problem 好吧,我仍然可以弄清楚,但是我找到了解决问题的方法

Timer now is in the activity and receiver is sending the event but after 10000 tries i want to trigger the END EVENT. 计时器现在处于活动状态,接收方正在发送事件,但是经过10000次尝试后,我想触发END EVENT。 Now the receiver since he didn't work i couldn't get it but now i Start an intent with extras for the same activity always with flags new_task and clear_top. 现在,由于接收器因为他不工作,所以我无法接收它,但是现在,我总是通过标记new_task和clear_top为同一个活动的附加功能开始意图。

No matter if my receiver is working or not, since the service is ok i will start the specific Activity and pseudo-show the END EVENT. 无论我的接收器是否工作,由于服务正常,我将启动特定的“活动”并以伪方式显示“结束事件”。

PS:: This behavior isn't always trigger but sometimes. PS ::这种行为并非总是会触发,但有时会触发。 So now i am ok. 所以现在我很好。

If i am not understood please comment and ask anything. 如果我不理解,请发表评论并提出任何问题。 Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM