How to code a “sync now” operation on Android?

别来无恙 提交于 2019-12-04 12:35:22

问题


I have a sync adapter for my DB and at certain points in the application usage, it needs to sync the DB now. I would like to trigger the sync adapter as if its sync-time popped and then re-establish the sync time (i.e., 4 hours from this 'sync now" event).


回答1:


I think (not tested) a better solution is already defined in the ContentResolver.

ContentResolver.requestSync(account, authority, extras);

so one might do the following:

AccountManager am = AccountManager.get(context);
Account account =  null;
am.getAccountsByType(mytype);
for(Account a : accounts) {
    if (am.getUserData(account, key)) {
        account = a;
        break;
    }
}
Bundle extras = new Bundle();
extras.putString(EXTRA_mystuff, myvalue);
ContentResolver.requestSync(account, authority, extras)


来源:https://stackoverflow.com/questions/4465765/how-to-code-a-sync-now-operation-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!