Sunday, April 21, 2013

[android help] BroadcastReceiver in a Service : Design Issues


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

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...