服务写入数据库并 activity 从中读取
Service writing to a database and activity reads from it
需要架构方面的帮助。我正在构建一个由 Service
和 Activity
组成的应用程序。 Service
将数据写入 SQLite
数据库,Activity
应读取数据并显示。有两个问题:1) Activity
应该即时接收更新。 2) 如果 Activity
进入后台,它应该在 returns 进入前台时加载所有更改。
经过两天的研究,我认为实现它的唯一方法是创建 ContentProvider
作为中间层并在 Activity 中使用 CursorLoader
。我对吗?还有其他建议吗?任何帮助,将不胜感激。谢谢。
另一种方法是实现自己的观察者模式。这是我的版本:
观察者
/**
* Receives notification of updates on database contents.
*/
public interface DatabaseObserver {
/**
* This method is called if the observable database's notifyObservers
* method is called (because the observable database has been updated.
*/
void onUpdate(String tableName);
}
可观察
/**
* Used to notify a group of Observer objects when a change occurs.
*/
public interface ObservableDatabase {
/**
* Adds the specified observer to the list of observers.
*/
void addObserver(DatabaseObserver observer);
/**
* Removes the specified observer from the list of observers.
*/
void removeObserver(DatabaseObserver observer);
}
数据库助手
public class DatabaseHelper extends SQLiteOpenHelper implements ObservableDatabase {
private static DatabaseHelper sInstance;
private CopyOnWriteArrayList<DatabaseObserver> mObservers = new CopyOnWriteArrayList<>();
/**
* Returns the single instance of Database helper
*/
public static synchronized DatabaseHelper getInstance(Context context) {
if (sInstance == null) {
sInstance = new DatabaseHelper(context.getApplicationContext());
}
return sInstance;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_STATEMENT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_STATEMENT);
onCreate(db);
}
/**
* Adds the specified observer to the list of observers.
*/
public void addObserver(DatabaseObserver observer) {
mObservers.addIfAbsent(observer);
}
/**
* Removes the specified observer from the list of observers.
*/
public void removeObserver(DatabaseObserver observer) {
mObservers.remove(observer);
}
private DatabaseHelper(Context context) {
super(context, DB_NAME, null, BuildConfig.VERSION_CODE);
}
private void notifyObservers(String tableName) {
for (DatabaseObserver observer: mObservers) {
observer.onUpdate(tableName);
}
}
}
请注意,您的数据库助手中的每个写入方法都应在修改数据库后调用notifyObservers(tableName)。
现在,任何组件都可以订阅要更新的数据库助手。在尝试更新 UI.
之前,确保 activity 在 onUpdate 回调中没有死
需要架构方面的帮助。我正在构建一个由 Service
和 Activity
组成的应用程序。 Service
将数据写入 SQLite
数据库,Activity
应读取数据并显示。有两个问题:1) Activity
应该即时接收更新。 2) 如果 Activity
进入后台,它应该在 returns 进入前台时加载所有更改。
经过两天的研究,我认为实现它的唯一方法是创建 ContentProvider
作为中间层并在 Activity 中使用 CursorLoader
。我对吗?还有其他建议吗?任何帮助,将不胜感激。谢谢。
另一种方法是实现自己的观察者模式。这是我的版本:
观察者
/**
* Receives notification of updates on database contents.
*/
public interface DatabaseObserver {
/**
* This method is called if the observable database's notifyObservers
* method is called (because the observable database has been updated.
*/
void onUpdate(String tableName);
}
可观察
/**
* Used to notify a group of Observer objects when a change occurs.
*/
public interface ObservableDatabase {
/**
* Adds the specified observer to the list of observers.
*/
void addObserver(DatabaseObserver observer);
/**
* Removes the specified observer from the list of observers.
*/
void removeObserver(DatabaseObserver observer);
}
数据库助手
public class DatabaseHelper extends SQLiteOpenHelper implements ObservableDatabase {
private static DatabaseHelper sInstance;
private CopyOnWriteArrayList<DatabaseObserver> mObservers = new CopyOnWriteArrayList<>();
/**
* Returns the single instance of Database helper
*/
public static synchronized DatabaseHelper getInstance(Context context) {
if (sInstance == null) {
sInstance = new DatabaseHelper(context.getApplicationContext());
}
return sInstance;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_STATEMENT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_STATEMENT);
onCreate(db);
}
/**
* Adds the specified observer to the list of observers.
*/
public void addObserver(DatabaseObserver observer) {
mObservers.addIfAbsent(observer);
}
/**
* Removes the specified observer from the list of observers.
*/
public void removeObserver(DatabaseObserver observer) {
mObservers.remove(observer);
}
private DatabaseHelper(Context context) {
super(context, DB_NAME, null, BuildConfig.VERSION_CODE);
}
private void notifyObservers(String tableName) {
for (DatabaseObserver observer: mObservers) {
observer.onUpdate(tableName);
}
}
}
请注意,您的数据库助手中的每个写入方法都应在修改数据库后调用notifyObservers(tableName)。
现在,任何组件都可以订阅要更新的数据库助手。在尝试更新 UI.
之前,确保 activity 在 onUpdate 回调中没有死