Receiver LifecycleA BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.This has important repercussions to what you can do in an onReceive(Context, Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes.In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver. For the former, you should instead use the NotificationManager API. For the latter, you can use Context.startService() to send a command to the service.Process LifecycleA process that is currently executing a BroadcastReceiver (that is, currently running the code in its onReceive(Context, Intent) method) is considered to be a foreground process and will be kept running by the system except under cases of extreme memory pressure.Once you return from onReceive(), the BroadcastReceiver is no longer active, and its hosting process is only as important as any other application components that are running in it. This is especially important because if that process was only hosting the BroadcastReceiver (a common case for applications that the user has never or not recently interacted with), then upon returning from onReceive() the system will consider its process to be empty and aggressively kill it so that resources are available for other more important processes.This means that for longer-running operations you will often use a Service in conjunction with a BroadcastReceiver to keep the containing process active for the entire time of your operation.
You can find the answer from  http://developer.android.com and find it by yourself.
and ultrapro's answer is not right.

解决方案 »

  1.   

    动态注册的广播的生命周期依赖调用它的activity或service;
    静态注册的广播是常驻型的,即使应用程序退出了,仍然可以接收。内存不足时,当BroadcastReceiver执行完了onReceive方法后,是会被回收的。
      

  2.   

    先说说3楼,3楼大侠回复的都是英文,看的不是特别明白,它说重点是BroadcastReceiver不是长期活跃的,并且在系统空闲的时候会杀掉它;他下面又说把BroadcastReceiver和一个Service关联起来就可以保持活跃。不是十分明白,是不是说在Service里注册BroadcastReceiver就可以避免被杀掉?还是我翻译的不对呢?
    1楼说的前半部分我同意,但是我有个项目就是使用Service关联BraodcastReceiver捕获ACTION_TIME_TICK更新东西的,只要内存一有压力就不更新了,没有做具体的调试,我怀疑是系统把我的BraodcastReceiver给杀掉了,要不就是把我的Service杀了,OnDestroyed又把BroadcastReceiver给解除了。
    4楼的跟3楼说的差不多,我想知道如何避免BroadcastReceiver被系统杀掉呢
      

  3.   


    你的理解基本是正确的,把BroadcastReceiver放在service 里可以避免装这个BroadcastReceiver的进程被杀掉。原因是以为service 在android中具有较高的优先级,除了可见的activity,其次就是service最不容易被系统回收。
      

  4.   

    嗯,感谢fishmen26以及其他朋友的帮助。。
      

  5.   

    我也遇到类似的问题,我自己写了一个service,在onCreate方法里面注册receiver,这个receiver是用来接收ACTION_TIME_TICK这个action的,手机连接数据线的时候,会一直收到广播,一旦拔掉数据线,让手机黑屏待机,就好像收不到了,觉得太奇怪,麻烦大家指点一下,非常感谢。