I am writing a simple widget app. It consists of an AppWidgetProvider to receive the APP_WIDGET_UPDATE intent, a Service, and another programmatically registered Broadcast Receiver. The AppWidgetProvider only runs when the widget is loaded. The widget has a spot on the Home Screen where it receives update through the normal RemoteViews, and it plays sounds periodically. The RemoteViews update and sounds are done through the Service.
I have to use the programmatically registered (dynamic) Broadcast Receiver because I look for SCREEN_ON intents and they cannot be received by a static (manifest defined) one. The problem is that under Lollipop, it seems that Android kills my app after the phone has been idle for a while. Since the Service goes, so does the dynamic Broadcast Receiver. I tried registering it through the Application context, but it still gets killed.
I have also tried stopping my service when it finishes its tasks, instead of just keeping it running. It is designed so it doesn't matter if it gets stopped by Android. Upon restart, it reloads all info it needs to keep going. But Android eventually kills my process anyway.
Is it possible to keep the dynamic Broadcast Receiver from being killed? I have seen documents on the DONT_KILL_APP flag in the PackageManager, but am not sure if this is the thing to use. Or is there another intent that I could look for like SCREEN_ON that would work in a static Broadcast Receiver?