PDaria
PDaria

Reputation: 457

AppWidgetProvider: not called onEnabled method

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

Upvotes: 1

Views: 4597

Answers (2)

GalDude33
GalDude33

Reputation: 7130

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(...)

Upvotes: 7

CommonsWare
CommonsWare

Reputation: 1006644

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).

Upvotes: 3

Related Questions