Android SQLite DB notifications

前端 未结 3 733
梦谈多话
梦谈多话 2020-12-15 21:47

I am writing an Android app that needs to be notified whenever a given SQLite database changes (any new row added, deleted or updated).

Is there any programmatic wa

3条回答
  •  半阙折子戏
    2020-12-15 22:43

    You can use also use the getContentResolver().registerContentObserver but unfortunately it doesn't tell you what kind of change was made, it could be a delete, insert or update.

    If you control the ContentProvider that interfaces with the DB then you could fire an Intent or use getContentResolver().notifyChange to send a special Uri notification that identifies both the table and action. An example Uri you could notify with might be: content://my-authority/change/table-name/insert

    But even then you don't know exactly which rows were effected by the change.

    Seems like triggers that write to a change log table will guarantee you hear about all changes regardless of where they came from, and you can know the exact id and action that occurred. Unfortunately it means slower inserts/updates/deletes and it means you probably need a Service of some kind to process and delete changes.

    I'd love to hear if these is some better solution out there!

提交回复
热议问题