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.
You can find the answer from http://developer.android.com and find it by yourself.
and ultrapro's answer is not right.
静态注册的广播是常驻型的,即使应用程序退出了,仍然可以接收。内存不足时,当BroadcastReceiver执行完了onReceive方法后,是会被回收的。
1楼说的前半部分我同意,但是我有个项目就是使用Service关联BraodcastReceiver捕获ACTION_TIME_TICK更新东西的,只要内存一有压力就不更新了,没有做具体的调试,我怀疑是系统把我的BraodcastReceiver给杀掉了,要不就是把我的Service杀了,OnDestroyed又把BroadcastReceiver给解除了。
4楼的跟3楼说的差不多,我想知道如何避免BroadcastReceiver被系统杀掉呢
你的理解基本是正确的,把BroadcastReceiver放在service 里可以避免装这个BroadcastReceiver的进程被杀掉。原因是以为service 在android中具有较高的优先级,除了可见的activity,其次就是service最不容易被系统回收。