我正在构建一个遵循IOSched检索数据方式的应用程序,除了我认为我会使用CursorLoader
而不是ContentObserver
:
我也一直在参考 Reto 的android-protips-location它确实使用CursorLoader
并且逻辑流程与 IOSched 非常相似,因此:
initLoader → startService (serviceIntent) → handleIntent → insert into DB → notifyChange → onLoadFinished → update UI
我期望看到的是在数据库上执行一次后CursorLoader
返回 a 。Cursor
insert
目前,片段onActivityCreated
调用initLoader
并运行查询ContentProvider
this 返回该Cursor
时间点的当前数据。但是,当我执行刷新时似乎onLoadFinished
没有被触发。日志显示delete
并insert
在ContentProvider
执行时执行,但查看日志显示notifyChange
在插入时调度。
// in my Fragment:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(0, null, this);
refreshWelcome();
}
public void refreshWelcome() {
Intent i = new Intent(getActivity(), SyncService.class);
i.setAction(SyncService.GET_WELCOME);
getActivity().startService(i);
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri queryUri = AppContract.Welcome.CONTENT_URI;
String[] projection = new String[] { Welcome.WELCOME_FIRST_NAME };
String where = null;
String[] whereArgs = null;
String sortOrder = null;
// create new cursor loader
CursorLoader loader = new CursorLoader(getActivity(), queryUri, projection, where, whereArgs, sortOrder);
return loader;
}
//in AppProvider (which extends ContentProvider)
@Override
public Uri insert(Uri uri, ContentValues values) {
final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
final int match = sUriMatcher.match(uri);
switch (match) {
case WELCOME: {
long rowId = db.insertOrThrow(Tables.WELCOME, null, values);
if (rowId > 0) {
getContext().getContentResolver().notifyChange(uri, null);
return uri;
}
}
}
return null;
}