My App needs to listen to a broadcast event (HEADSET events) and act accordingly. I discovered that HEADSET events cannot be used via a minifest receiver as the flag FLAG_RECEIVER_REGISTERED_ONLY is enabled for this event. So, you can only register to this event dynamically inside your program. As my App would not be active during the events, I needed a service to handle this event , I register in onCreate and unregister in onDestroy. So far, so good. I implemented the service and started testing it, I discovered that the service can be killed by the system in case of a resource crunch and the system will automatically restart the service. However, the surprising thing is that the delay between kill and restart, which is completely random and could be HUGE. Sometimes, it was in seconds and other times it was in hours. So, you can never be sure that your service is running when the broadcast event happens !
I have browsed stackoverflow on that there were various suggestions. Have a dummy thread that does nothing to increase "importance" of the service, use service with startForeground - which causes a Notification. All these look like workarounds and not foolproof.
Is there any way of solving this in a foolproof manner ? What can be alternate solutions for handling "REGISTERED_ONLY" broadcast events ?
Thanks.
.
stackoverflow.comm
No comments:
Post a Comment