简体   繁体   中英

how to listen for SQLite database changes in android

I'm using SQLite database in android and want to listening for any database changes. How can I do this?

Thanks for all future help!

In fact, SQLite offers such functionality: SQLite Data Change Notification Callbacks

How it can be used in Android is another story though..

SQLite doesn't offer any change listener functionality; you have to monitor it yourself. The simplest way to achieve this would be to send a Broadcast (or even better, a LocalBroadcast) anytime you modify the database. Some of the database libraries already offer functionality that is similar to this - check out GreenDAO .

a simple implementation of changeListener for the database on Android

suppose that you have a class to handle your queries in your android app, we need to make the database methods observable . and also we need some listeners to observe the abovementioned observable. let's make the database handler observable :

let's make the observable interface:

public interface DatabaseObservable {
    //register the observer with this method
    void registerDbObserver(DatabaseObserver databaseObserver);
    //unregister the observer with this method
    void removeDbObserver(DatabaseObserver databaseObserver);
    //call this method upon database change
    void notifyDbChanged();

}

now implement the observable in your database class

public class LocalStorageDb extends SQLiteOpenHelper implements DatabaseObservable {
LocalStorageDb lDb;

//make it Singleton
public static synchronized LocalStorageDB getInstance(Context context) {
    if (mlLocalQuickChatDB == null) {
        mlLocalQuickChatDB = new LocalStorageDB(context.getApplicationContext());
    }
    return mlLocalQuickChatDB;
}

//there are some methods to do some queries
    public void createContact(Foo foo, Bar bar){

//some queries here

//call the Observable Method to let know the observers that it has changed
onDatabaseChanged();
}

//now override the DatabaseObservable method which is responsible to notify the listeners
@Override
public void onDatabaseChanged() {
    for (DatabaseObserver databaseObserver:observerArrayList){
if (databaseObserver!= null){
    databaseObserver.onDatabaseChanged();
    }}
}
//also you need functions to **register** or **unregister** the observers:
 @Override
public void registerDbObserver(DatabaseObserver databaseObserver) {
    if (!observerArrayList.contains(databaseObserver)){
        observerArrayList.add(databaseObserver);
    }

@Override
public void removeDbObserver(DatabaseObserver databaseObserver) {
    observerArrayList.remove(databaseObserver);
}

then we need an observer to observe the changes:

public interface DatabaseObserver {
void onDatabaseChanged();
}

now in your activity or fragment, there is a function to fetch the changes, like getLocalContact . implement the observer on the fragment for example:

public class ExampleFragment extends Fragment implements DatabaseObserver {

LocalStorageDB localStorageDB;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    localStorageDB = LocalStorageDB.getInstance();
}

@Override
public void onPause() {
    super.onPause();
    localStorageDB.removeObserver(this);

}


@Override
public void onResume() {
    localStorageDB.registerObserver(this);
    super.onResume();
}

public ExampleFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_example, container, false);
}

@Override
public void onDatabaseChanged() {
getLocalContact();
}

private void getLocalContact(){
    //function to fetch contacts from database
}
}

Hi I'd like to add a new suggestion Incase of firebase usage. You can make a new json node for users messages and use On Data Change to detect the new number of unread messages then update your ui in the onDatachange. Is that smart or far away from the main idea?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM