4

How can I register my App Widget (home screen widget) to listen to local broadcast messages that send over LocalBroadcastManager?

nrofis
  • 8,975
  • 14
  • 58
  • 113

1 Answers1

4

Your app widget really is a set of Views in another app's process (e.g., the home screen). Hence, there is nothing in your process that can really "listen" to anything.

When you want to update the app widget, just update it, by using an AppWidgetManager and updateAppWidget().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Widget provider is broadcast reciever. I just want to know if there any way to receive `LocalBroadcast` message – nrofis Oct 10 '13 at 19:14
  • @nrofis: "Widget provider is broadcast reciever" -- only for the purposes of responding to system events related to the app widget, such as when it is being removed. And, since a manifest-registered `BroadcastReceiver` lives for a millisecond or so and goes away, it cannot listen to `LocalBroadcastManager` messages. When you want to update the app widget, just update it, by using an `AppWidgetManager` and `updateAppWidget()`. – CommonsWare Oct 10 '13 at 19:17
  • I see... So the system "calls" to `onUpdate` sometimes and then I need to check the values that I want to show? – nrofis Oct 10 '13 at 19:28
  • 1
    @nrofis: `onUpdate()` will be triggered when the app widget is first placed on the screen, every so often based on your `android:updatePeriodMillis` value in your app widget XML metadata, and a few other scenarios. You are welcome to update your app widget whenever you want using an `AppWidgetManager` and `updateAppWidget()`, as I have stated repeatedly. – CommonsWare Oct 10 '13 at 19:33
  • Thanks! You really helped me to understand it! – nrofis Oct 10 '13 at 19:39
  • I have an AppWidgetProvider where I register a local broadcast receiver in the `onEnabled` method and deregister it in the `onDisabled` method. It works but after some random time the receiver is gone. In my app I have other local receivers for this action and they are working fine. Could it be that the provider is being garbage collected? – Mister Smith Oct 23 '14 at 08:32
  • @MisterSmith: "Could it be that the provider is being garbage collected?" -- yes, plus eventually your process is terminated. Your `AppWidgetProvider` will live exactly one call to some callback method (e.g., `onUpdate()`), as it is a manifest-registered `BroadcastReceiver`. Anything you are trying to do beyond that will be unreliable. If you want to update the app widget separately from `onUpdate()`, use `AppWidgetManager` and update it whenever you want. – CommonsWare Oct 23 '14 at 10:54