Android SQLite DB notifications

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 way to listen to these notifications ?

  • hardcode string vs @string in Java code - Android
  • Button Onclick Listener in included layouts
  • Android: Execution failed for task ':app:processDebugResources'
  • how to create a folder in android External Storage Directory?
  • adding multiple entries to a HashMap at once in one statement
  • Android - Facebook SDK 3 - How to login programmatically without LoginButton
  • Is writing DB triggers for each table the only way ?

    Related posts:

    VoIP library for Android
    Compatiblity of Material Design to versions below Android 5.0?
    wrap_content width on mutiline TextView
    Set FAB icon color
    How to pass a parameter to the Java code in run/debug configuration from Android Studio
    Why is it necessary to use @+id instead of @id?
  • Android Google Map API V2 : Open Custom Info Window on Right Side of Marker
  • Android window doesn't resize/pan when softinput opened from WebView
  • Android: Overlay on Window over Activities of a Task
  • Why does LinearLayout instance.getLayoutParams look to have a wrong class?
  • Android 4.4 translucent Status and Navigation bars style on Android 5.0
  • EditText setError message does not clear after input
  • 3 Solutions collect form web for “Android SQLite DB notifications”

    SQLite provides Data Change Notification Callbacks. I don’t think that Android exposes them directly but it does have for example CursorAdapter which provides some change notifications.

    As thinksteep asked however, do you expect your DB to be changed outside the scope of your own application?

    You can register an observer class such as DataSetObserver

    Then whenever you change something you can call cursor.registerDataSetObserver(..) to register observe changes.

    It’s not well documented but I’m sure that there are some examples out there

    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!

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.