JJD*_*JJD 10 android synchronization callback android-contentresolver android-syncadapter
在我的Android项目中,我使用自定义SyncAdapter来下载数据并将其存储在本地SQLite数据库中.
public class CustomSyncAdapter extends AbstractThreadedSyncAdapter {
public CustomSyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
}
@Override
public void onPerformSync(Account account,
Bundle extras,
String authority,
ContentProviderClient provider,
SyncResult syncResult) {
// 1) Download data via AsyncTask
// 2) Store data via ContentProvider
}
}
Run Code Online (Sandbox Code Playgroud)
我Activity使用ContentResolver诸如... 来安排主要的同步
ContentResolver.requestSync(account, Authentication.AUTHORITY, bundle);
Run Code Online (Sandbox Code Playgroud)
Activity在同步完成时是否有通用方式通知呼叫?
你可以用它来做到这一点ContentResolver.notifyChange().
所以在你的意思SyncAdapter是这样的:
...
@Override
public void onPerformSync(Account account,
Bundle extras,
String authority,
ContentProviderClient provider,
SyncResult syncResult) {
// 1) Download data via AsyncTask
// 2) Store data via ContentProvider
getContext().getContentResolver().notifyChange(<your_content_uri>, null, false);
}
...
Run Code Online (Sandbox Code Playgroud)
然后在Activity中使用ContentResolver.registerContentObserver():
public class MyActivity extends Activity {
private ContentObserver mObserver;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mObserver = new ContentObserver(new Handler(Looper.getMainLooper())) {
public void onChange(boolean selfChange) {
// Do something.
}
};
getContentResolver().registerContentObserver(<your_content_uri>, mObserver);
}
@Override
public void onDestroy() {
getContentResolver().unregisterContentObserver(mObserver);
}
}
Run Code Online (Sandbox Code Playgroud)