15

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 ?

Is writing DB triggers for each table the only way ?

1
  • SQLLite database operations are those from your app only (or) could be from anywhere? Commented Jan 9, 2012 at 5:17

3 Answers 3

5

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?

Sign up to request clarification or add additional context in comments.

3 Comments

As thinksteep asked however, do you expect your DB to be changed outside the scope of your own application? very helpful notice
What change notifications does CursorAdapter provide?
CursorAdapter's notifications are based on Android stuff that is unrelated to database changes directly. It is up to the programmer to notify that mechanism when the database changes.
1

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

2 Comments

From what I understand, the cursor provides a snapshot of the data. This means that unless you re-query the data, the listener won't be invoked. From the documentation: "Register an observer that is called when changes happen to the contents of the this cursors data set, for example, when the data set is changed via requery(), deactivate(), or close()."
This triggers when the programmer calls content provider.notifyChange, and doesn't auto detect changes.
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.