0

I am trying to force sync for all the accounts that show under sync options. This is the code i have tried so far which leads to crash. How can i trigger android to sync all accounts? I want the same effect as when the user is in the sync settings and clicks "sync all".

     final String AUTHORITY = "com.example.android.datasync.provider";
     AccountManager account_manager=AccountManager.get(this);
     Account[] accounts = account_manager.getAccounts();
     ContentResolver.setMasterSyncAutomatically(true);
     for(int i=0;i<accounts.length;i++)
        ContentResolver.requestSync(accounts[i], AUTHORITY, null);
Kanwaljit Singh
  • 4,339
  • 2
  • 18
  • 21
Anonymous
  • 4,470
  • 3
  • 36
  • 67

1 Answers1

4

Third parameter of ContentResolver.requestSync() should not be null. You should call it like this:

ContentResolver.requestSync(accounts[i], AUTHORITY, new Bundle());

If you want sync to execute immediately you should call it like this:

Bundle bundle = new Bundle()
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
ContentResolver.requestSync(accounts[i], AUTHORITY, bundle);
Blaz
  • 1,935
  • 15
  • 14
  • thank you for your answer. you definately fixed the crash! but i don't think it's synching...after i press the button in my app i am immediately redirected to sync settings by intent. and the button in settings says "sync all" not "cancel sync". and there is no sync icon on the notification bar either. – Anonymous Dec 28 '13 at 12:58
  • `ContentResolver.requestSync()` just tells system that account has to be synced, then it is up to system to trigger sync request. In my experience this happens within one minute if network connection is available, depending on when privius sync was triggered and probably a lot of other factors. – Blaz Dec 28 '13 at 13:10
  • i see...so there is nothing else i can do to speed the process up. – Anonymous Dec 28 '13 at 13:16
  • 1
    i just added some code to log the isSyncActive(accounts[i], AUTHORITY) and isSyncPending(accounts[i], AUTHORITY) values.they all turn out false – Anonymous Dec 28 '13 at 13:18
  • @user3051067 - did you find answer to your question? [same problem here] – user1406716 Mar 28 '14 at 23:56
  • 1
    unfortunately no i did not...i just turn on the automatic synch and after a while it synchs by its self. If you find a solution please share! – Anonymous Apr 01 '14 at 11:13