react-native-android-widget icon indicating copy to clipboard operation
react-native-android-widget copied to clipboard

🚀 [Feature Request] Enable Frequent Widget Updates via BroadcastReceiver

Open drminh2807 opened this issue 8 months ago • 1 comments

First and foremost, I want to express my sincere gratitude for your work on the react-native-android-widget library.

Description

Currently, the react-native-android-widget library offers two methods for updating widget:

  1. updatePeriodMillis : This method schedules periodic updates but is limited to a minimum interval of 30 minutes, as per Android's restriction.

  2. requestWidgetUpdate: Allows manual updates when the app is activity However, for widgets that display real-time information such as clocks, timers, or live data feeds, these methods are insufficient due to their infrequency or reliance on the app being open.

Proposed Enhancement

Introduce support for updating widgets using BroadcastReceiver, enabling more frequent and responsive updates. This can be achieved by:

  • Listening to System Broadcasts: Register a BroadcastReceiver to listen for system events like ACTION_TIME_TICK, which is broadcast every minute. This allows the widget to update in response to these event.

  • Using AlarmManager: Schedule regular updates by setting alarms that trigger the BroadcastReceiver at desired intervals, even when the app is not active

  • Implementing goAsync(): Within the onReceive() method of the BroadcastReceiver, use goAsync() to perform asynchronous operations without blocking the main thread, ensuring smooth update.

References

drminh2807 avatar Apr 17 '25 07:04 drminh2807

Two out of the three provided references are dead links :)

I'm not sure if adding broadcast receivers should be a part of this library, since it adds more complexity and is not strictly related to widgets. Maybe it can be implemented in a different library, or as custom code.

What I might do is add a native helper in the library, that when called will call the JS widget task handler.

sAleksovski avatar Apr 19 '25 10:04 sAleksovski

I just released 0.17.0 that implements this feature.

See https://saleksovski.github.io/react-native-android-widget/docs/update-widget#native-rnwidgetjscommunicationrequestwidgetupdate

sAleksovski avatar May 16 '25 21:05 sAleksovski