1

I have widget that display data from content provider. I want to know when data in content provider changes. As far as I know way to do it is

context.getContentResolver().registerContentObserver

But AppWidgetProvider.onEnabled method is not called when I add first instance of the widget. That's why I can't make registerContentObserver. The same with onDisabled.

How to solve this problem?

Thanks

PDaria
  • 457
  • 10
  • 21

2 Answers2

6

You need to add android.appwidget.action.APPWIDGET_ENABLED as another action:

<intent-filter>
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    <action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
    <action android:name="android.appwidget.action.APPWIDGET_DELETED" />
    <action android:name="android.appwidget.action.APPWIDGET_DISABLED" />  
</intent-filter>

Without that, you will not receive the broadcast that triggers onEnabled().

note: APPWIDGET_DELETED for onDeleted(...), APPWIDGET_DISABLED for onDisabled(...)

GalDude33
  • 7,071
  • 1
  • 28
  • 38
  • 1
    Is this really correct? The official sample WeatherListWidget handles onEnabled, but does not have this Action in its manifest. – Tom anMoney Feb 05 '13 at 07:35
  • 1
    I just verified this, but, at least on my Galaxy Nexus 4.2.1, onEnabled is called even if the intent-filter is not present. – Tom anMoney Feb 05 '13 at 07:46
  • 2
    From the official documentation: "... it specifies that the AppWidgetProvider accepts the ACTION_APPWIDGET_UPDATE broadcast. This is the only broadcast that you must explicitly declare. The AppWidgetManager automatically sends all other App Widget broadcasts to the AppWidgetProvider as necessary." – Zsolt Safrany Aug 11 '13 at 17:08
3

An AppWidgetProvider (or any other manifest-registered BroadcastReceiver) cannot call registerContentObserver(). The entity that is changing your content will need to update your app widget, or you will need to implement some sort of polling mechanism (e.g., check for new content based on android:updatePeriodMillis).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491