简体   繁体   English

如何在android中监听SQLite数据库的变化

[英]how to listen for SQLite database changes in android

I'm using SQLite database in android and want to listening for any database changes.我在 android 中使用 SQLite 数据库并想监听任何数据库更改。 How can I do this?我怎样才能做到这一点?

Thanks for all future help!感谢所有未来的帮助!

In fact, SQLite offers such functionality: SQLite Data Change Notification Callbacks事实上,SQLite 提供了这样的功能: SQLite Data Change Notification Callbacks

How it can be used in Android is another story though..不过,如何在 Android 中使用它是另一回事。

SQLite doesn't offer any change listener functionality; SQLite 不提供任何更改监听器功能; 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 .一些数据库库已经提供了与此类似的功能 - 查看GreenDAO

a simple implementation of changeListener for the database on Android Android上数据库的changeListener的简单实现

suppose that you have a class to handle your queries in your android app, we need to make the database methods observable .假设您有一个类来处理您的 android 应用程序中的查询,我们需要使数据库方法可观察 and also we need some listeners to observe the abovementioned observable.而且我们还需要一些听众来观察上述可观察的。 let's make the database handler observable :让我们让数据库处理程序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现在在你的数据库类中implement observable

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:那么我们需要一个observer观察变化:

public interface DatabaseObserver {
void onDatabaseChanged();
}

now in your activity or fragment, there is a function to fetch the changes, like getLocalContact .现在在您的活动或片段中,有一个获取更改的函数,例如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.嗨,我想添加一个新的建议,以防使用 firebase。 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.您可以为用户消息创建一个新的 json 节点,并使用 On Data Change 来检测新的未读消息数量,然后在 onDatachange 中更新您的 ui。 Is that smart or far away from the main idea?这是聪明的还是远离主要思想?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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