androidbroadcastreceiverandroid-broadcastandroid-appwidgetappwidgetprovider

How long does a BroadcastReceiver stay active if it is registered in an AppWidgetProvider?


I'm registered a BroadcastReceiver for the ACTION_TIME_TICK inside my AppWidgetProvider to update a clock every minute. Like in this example:

AFAIK a BroadcastReceiver that is registered at runtime is only active as long as the application is running. But from my understanding the AppWidgetProvider could get garbage collected at any time and it doesn't have a real lifecycle anyway.

For the last 30 minutes this has been working great but is this really (still?) a viable method?
How long will the BroadcastReceiver stay registered in theory?


Update: I simulated a ram shortage with a memory filler app and checked the list of running processes with adb shell ps -A. Once the process was terminated the updates to the clock did in fact stop.


Solution

  • How long will the BroadcastReceiver stay registered in theory?

    For as long as your process is running. How long your process will remain running varies widely based on environment (amount of system RAM, number of installed applications doing background work by one means or another, whether the user is actively using the device and flipping between other apps, etc.).