17

I'v created a Contentprovide and implements it's update() method like this:

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    final SQLiteDatabase db = mHelper.getWritableDatabase();
    int count = db.update(InslideDB.FOLDER_TABLE, values, selection, selectionArgs);

    /* To notify the observer */
    this.getContext().getContentResolver().notifyChange(URI, null);  
    return count;
}

How can I get notified in my appwidget? Thanks!

I'm trying this Resolver but no Log (--------------------Date Changed) print:

class DataProviderObserver extends ContentObserver {
    private AppWidgetManager mAppWidgetManager;
    private ComponentName mComponentName;

    DataProviderObserver(AppWidgetManager mgr, ComponentName cn, Handler h) {
        super(h);
        mAppWidgetManager = mgr;
        mComponentName = cn;
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);  //NOTE: Have to call this. dwy.

        // The data has changed, so notify the widget that the collection view needs to be updated.
        // In response, the factory's onDataSetChanged() will be called which will requery the
        // cursor for the new data.

        Log.d("PresenterWidgetProvider", "--------------------Date Changed");

        mAppWidgetManager.notifyAppWidgetViewDataChanged(
                mAppWidgetManager.getAppWidgetIds(mComponentName), R.id.inslide_grid);
    }
}


public class PresenterWidgetProvider extends AppWidgetProvider{

    private static final String AUTHORITY = "com.apps.slide";

    static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/folder");

    public static final String EXTRA_INSLIDE_PATH = "EXTRA_WIDGET_PATH";
    private static HandlerThread sWorkerThread;
    private static Handler sWorkerQueue;
    private static DataProviderObserver sDataObserver;

    public PresenterWidgetProvider () {

        // Start the worker thread to update Content Provider.
        sWorkerThread = new HandlerThread("PresenterWidgetProvider worker");
        sWorkerThread.start();
        sWorkerQueue = new Handler(sWorkerThread.getLooper());

    }

    @Override
    public void onEnabled(Context context) {
        // Register for external updates to the data to trigger an update of the widget.  When using
        // content providers, the data is often updated via a background service, or in response to
        // user interaction in the main app.  To ensure that the widget always reflects the current
        // state of the data, we must listen for changes and update ourselves accordingly.
        final ContentResolver r = context.getContentResolver();
        if (sDataObserver == null) {
            final AppWidgetManager mgr = AppWidgetManager.getInstance(context);
            final ComponentName cn = new ComponentName(context, PresenterWidgetProvider.class);
            sDataObserver = new DataProviderObserver(mgr, cn, sWorkerQueue);
            r.registerContentObserver(CONTENT_URI, true, sDataObserver);
            Log.d("PresenterWidgetProvider", "--------------------onEnabled. sDataObserver = " + sDataObserver);
        }
    }

So, I finally solved it by 4 steps:

  1. Add this.getContext().getContentResolver().notifyChange(URI, null);

to ContentProvider's updat() insert() delete() callbacks.

  1. Create a new Observer and new Thread to handle it.

  2. Call notifyAppWidgetViewDataChanged() in Observer's onChange().

  3. Delete the widget and place a new one.

That's it.

Tur1ng
  • 506
  • 4
  • 18
herbertD
  • 9,587
  • 12
  • 46
  • 74
  • 1
    Thanks for sharing this. I think it is not enough to register the observer in onEnabled, since onEnabled is not called again after device is restarted. I'd put it into onReceive. – mtotschnig May 24 '14 at 10:52

1 Answers1

18

You can use the ContentObserver class. You just have to implement that class and use the getContentResolver().registerContentObserver() method to register the observer.

Cristian
  • 191,931
  • 60
  • 351
  • 260
  • @Ctristian Thanks! And I've use the ContentObserver but it didn't call callback onChange(). I've update the code to questions. I'm get a little cofused about how to use Observer and how to update widget then. Thanks again! – herbertD Jun 28 '12 at 05:06
  • 3
    I think you cannot register the observer inside your `AppWidgetProvider`. Instead, start a service from your `AppWidgetProvider`, and register your `ContentObserver` from there. – Cristian Jun 28 '12 at 05:34
  • I have solved it. The complete code is updated above. Thank you. – herbertD Jun 28 '12 at 08:53
  • @Cristian I have similar query. In my application I am using content provider :-android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI to list the image files. Now I want that whenever this content provider data gets changed, I want to Toast a message to user. How can I do that. Please help – Ankesh kumar Jaisansaria May 02 '16 at 08:27