public static void configureSyncFor(Account account) { Log.d(TAG, "Configuring account sync"); ContentResolver.setIsSyncable(account, BuildConfig.PROVIDER_AUTHORITY_SYNC, 1); ContentResolver.setSyncAutomatically(account, BuildConfig.PROVIDER_AUTHORITY_SYNC, true); ContentResolver.addPeriodicSync(account, BuildConfig.PROVIDER_AUTHORITY_SYNC, new Bundle(), SYNC_PERIOD); }
@Test public void shouldGetPeriodSyncs() { assertThat(ContentResolver.getPeriodicSyncs(a, AUTHORITY).size()).isEqualTo(0); ContentResolver.addPeriodicSync(a, AUTHORITY, new Bundle(), 6000L); List<PeriodicSync> syncs = ContentResolver.getPeriodicSyncs(a, AUTHORITY); assertThat(syncs.size()).isEqualTo(1); PeriodicSync first = syncs.get(0); assertThat(first.account).isEqualTo(a); assertThat(first.authority).isEqualTo(AUTHORITY); assertThat(first.period).isEqualTo(6000L); assertThat(first.extras).isNotNull(); }
@Test public void shouldAddPeriodicSync() { Bundle fooBar = new Bundle(); fooBar.putString("foo", "bar"); Bundle fooBaz = new Bundle(); fooBaz.putString("foo", "baz"); ContentResolver.addPeriodicSync(a, AUTHORITY, fooBar, 6000L); ContentResolver.addPeriodicSync(a, AUTHORITY, fooBaz, 6000L); ContentResolver.addPeriodicSync(b, AUTHORITY, fooBar, 6000L); ContentResolver.addPeriodicSync(b, AUTHORITY, fooBaz, 6000L); assertThat(ShadowContentResolver.getPeriodicSyncs(a, AUTHORITY)).containsExactly( new PeriodicSync(a, AUTHORITY, fooBar, 6000L), new PeriodicSync(a, AUTHORITY, fooBaz, 6000L)); assertThat(ShadowContentResolver.getPeriodicSyncs(b, AUTHORITY)).containsExactly( new PeriodicSync(b, AUTHORITY, fooBar, 6000L), new PeriodicSync(b, AUTHORITY, fooBaz, 6000L)); // If same extras, but different time, simply update the time. ContentResolver.addPeriodicSync(a, AUTHORITY, fooBar, 42L); ContentResolver.addPeriodicSync(b, AUTHORITY, fooBaz, 42L); assertThat(ShadowContentResolver.getPeriodicSyncs(a, AUTHORITY)).containsExactly( new PeriodicSync(a, AUTHORITY, fooBar, 42L), new PeriodicSync(a, AUTHORITY, fooBaz, 6000L)); assertThat(ShadowContentResolver.getPeriodicSyncs(b, AUTHORITY)).containsExactly( new PeriodicSync(b, AUTHORITY, fooBar, 6000L), new PeriodicSync(b, AUTHORITY, fooBaz, 42L)); }
assertThat(ShadowContentResolver.getPeriodicSyncs(a, AUTHORITY)).isEmpty(); ContentResolver.addPeriodicSync(a, AUTHORITY, fooBar, 6000L); ContentResolver.addPeriodicSync(a, AUTHORITY, fooBaz, 6000L); ContentResolver.addPeriodicSync(a, AUTHORITY, foo42, 6000L); ContentResolver.addPeriodicSync(b, AUTHORITY, fooBar, 6000L); ContentResolver.addPeriodicSync(b, AUTHORITY, fooBaz, 6000L); ContentResolver.addPeriodicSync(b, AUTHORITY, foo42, 6000L);
//Create Account mAccount = createSyncAccount(this); //Turn on periodic syncing ContentResolver resolver = getContentResolver(); resolver.setIsSyncable(mAccount, AUTHORITY, 1); resolver.setSyncAutomatically(mAccount, AUTHORITY, true); resolver.addPeriodicSync( mAccount, AUTHORITY, Bundle.EMPTY, 30);
static void setSyncFrequencySeconds(Account androidAccount, long syncFrequencySeconds) { // See // http://developer.android.com/reference/android/content/ContentResolver.html#addPeriodicSync(android.accounts.Account, java.lang.String, android.os.Bundle, long) // and // http://stackoverflow.com/questions/11090604/android-syncadapter-automatically-initialize-syncing if (syncFrequencySeconds != getSyncFrequencySeconds(androidAccount)) { ContentResolver.removePeriodicSync(androidAccount, MatchedUri.AUTHORITY, Bundle.EMPTY); if (syncFrequencySeconds > 0) { ContentResolver.addPeriodicSync(androidAccount, MatchedUri.AUTHORITY, Bundle.EMPTY, syncFrequencySeconds); } } }
public void setSyncPeriodic(String authority, long interval_in_minute, long seconds_per_minute, long milliseconds_per_second) { Account account = mUser.getAccount(); Bundle extras = new Bundle(); this.setAutoSync(authority, true); ContentResolver.setIsSyncable(account, authority, 1); final long sync_interval = interval_in_minute * seconds_per_minute * milliseconds_per_second; ContentResolver.addPeriodicSync(account, authority, extras, sync_interval); }
/** * 利用帐号同步机制拉活 * @param context */ public static void startAccountSync(Context context){ String accountType = "com.henrik.keeplive"; AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE); Account account = null; Account[] accounts = accountManager.getAccountsByType(accountType); if(accounts.length>0){ account = accounts[0]; }else { account = new Account("test", accountType); } if(accountManager.addAccountExplicitly(account,null,null)){ String authority = accountType; long sync_interval = 15*60; ContentResolver.setIsSyncable(account,authority,1); ContentResolver.setSyncAutomatically(account, authority, true); //自动同步 ContentResolver.addPeriodicSync(account, authority, new Bundle(), sync_interval);//设置同步时间间隔 } }
/** * Schedule syncAdapter by interval * * @param intervalInMinute interval in minute */ protected boolean addPeriodicSync(int intervalInMinute) { boolean added = false; if (!periodicSyncExists(intervalInMinute)) { Bundle bundle = new Bundle(); // remove all others removePeriodicSync(); ContentResolver.setIsSyncable(mAccount, mAuthority, 1); ContentResolver.setSyncAutomatically(mAccount, mAuthority, true); long syncInSecond = intervalInMinute * SECONDS_PER_MINUTE; ContentResolver.addPeriodicSync( mAccount, mAuthority, bundle, syncInSecond); added = true; } return added; }
ContentResolver.addPeriodicSync(acct, OCSMSSettingsActivity._accountAuthority, b, syncFreq * 60);
ContentResolver.addPeriodicSync(acct, OCSMSSettingsActivity._accountAuthority, b, syncFreq * 60);
ContentResolver.addPeriodicSync(account, MainActivity.AUTHORITY, Bundle.EMPTY, setAuthBundle(accountToChange, serverInfo, intervall); ContentResolver.addPeriodicSync(accountToChange, MainActivity.AUTHORITY, Bundle.EMPTY,
public void updatePeriodicSync(MovirtAccount account, boolean syncEnabled) { ObjectUtils.requireNotNull(account, "account"); String authority = OVirtContract.CONTENT_AUTHORITY; Bundle bundle = Bundle.EMPTY; try { ContentResolver.setSyncAutomatically(account.getAccount(), OVirtContract.CONTENT_AUTHORITY, syncEnabled); if (syncEnabled) { long intervalInSeconds = environmentStore.getSharedPreferencesHelper(account).getPeriodicSyncInterval() * (long) Constants.SECONDS_IN_MINUTE; ContentResolver.addPeriodicSync(account.getAccount(), authority, bundle, intervalInSeconds); } else { ContentResolver.removePeriodicSync(account.getAccount(), authority, bundle); } } catch (AccountDeletedException ignored) { } }
b.putInt("synctype", 1); ContentResolver.addPeriodicSync(account, getString(R.string.account_authority), b, DefaultPrefs.syncInterval * 60);
b.putInt("synctype", 1); ContentResolver.addPeriodicSync(account, getString(R.string.account_authority), b, DefaultPrefs.syncInterval * 60);
ContentResolver.setIsSyncable(account, AUTHORITY, 1); ContentResolver.setSyncAutomatically(account, AUTHORITY, true); ContentResolver.addPeriodicSync(account, AUTHORITY, new Bundle(), 60); Toast.makeText(getApplicationContext(), "Recreating this account is recommended to manage sync interval. Set to 15 minutes in the meantime", Toast.LENGTH_LONG).show();
ContentResolver.addPeriodicSync(account, Constants.SYNC_ACCOUNT_PLAYLIST_AUTHORITY, new Bundle(), 60L * syncInterval);
ContentResolver.addPeriodicSync(account, AUTHORITY, new Bundle(), SYNC_FREQUENCY); this.res.errorMessage = "Account has been modified"; return true; ContentResolver.addPeriodicSync(account, AUTHORITY, new Bundle(), SYNC_FREQUENCY); this.res.errorMessage = "Account has been added"; return true;