下面列出了android.content.ContentResolver#setSyncAutomatically ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void onAccountCreated(Account newAccount, Context context) {
/*
* Since we've created an account
*/
SunshineSyncAdapter.configurePeriodicSync(context, SYNC_INTERVAL, SYNC_FLEXTIME);
/*
* Without calling setSyncAutomatically, our periodic sync will not be enabled.
*/
ContentResolver.setSyncAutomatically(newAccount, context.getString(R.string.content_authority), true);
/*
* Finally, let's do a sync to get things started
*/
syncImmediately(context);
}
/**
* Updates ContentResolver sync settings for an Account's specified SyncAdapter.
*
* <p>Sets an accounts SyncAdapter (selected based on authority) to sync/not-sync automatically
* and immediately requests/cancels a sync.
*
* <p>updateSync should always be called under {@link AccountSnippet#mLock} write lock to avoid
* flapping between the getSyncAutomatically and setSyncAutomatically calls.
*
* @param account A Google Account.
* @param authority The authority of a content provider that should (not) be synced.
* @param sync Whether or not the account's content provider should be synced.
*/
private void updateSync(Account account, String authority, boolean sync) {
if (ContentResolver.getSyncAutomatically(account, authority) != sync) {
ContentResolver.setSyncAutomatically(account, authority, sync);
if (sync) {
ContentResolver.requestSync(account, authority, new Bundle());
} else {
ContentResolver.cancelSync(account, authority);
}
Log.i(
"Set sync to "
+ sync
+ " for account "
+ account
+ ", adapter "
+ authority
+ ".");
}
}
public static void initializePeriodicSync(Context context) {
// Enable Sync
ContentResolver.setIsSyncable(PredatorAccount.getAccount(context),
Constants.Authenticator.PREDATOR_ACCOUNT_TYPE,
Constants.Sync.ON);
ContentResolver.setSyncAutomatically(PredatorAccount.getAccount(context),
Constants.Authenticator.PREDATOR_ACCOUNT_TYPE,
true);
// Set periodic sync interval
Logger.d(TAG, "initializePeriodicSync: interval(seconds): " + PredatorSharedPreferences.getSyncIntervalInSeconds(context));
ContentResolver.addPeriodicSync(
PredatorAccount.getAccount(context),
Constants.Authenticator.PREDATOR_ACCOUNT_TYPE,
Bundle.EMPTY,
PredatorSharedPreferences.getSyncIntervalInSeconds(context));
}
private void removeAccount(final Account account) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
sharedPreferences.edit().remove("queueName").apply();
ContentResolver.setIsSyncable(account, WEBINSTALL_SYNC_AUTHORITY, 0);
ContentResolver.setSyncAutomatically(account, MyAccountActivity.WEBINSTALL_SYNC_AUTHORITY, false);
if(Build.VERSION.SDK_INT>=8){
ContentResolver.removePeriodicSync(account, MyAccountActivity.WEBINSTALL_SYNC_AUTHORITY, new Bundle());
}
mAccountManager.removeAccount(account, new AccountManagerCallback<Boolean>() {
@Override
public void run(AccountManagerFuture<Boolean> future) {
addAccount();
finish();
}
}, null);
}
/**
* Login successful. Show contacts activity
*/
static void onLoginSuccess(Activity activity, final Button button, final String uid) {
if (button != null) {
activity.runOnUiThread(new Runnable() {
public void run() {
button.setEnabled(true);
}
});
}
Account acc = Utils.getSavedAccount(AccountManager.get(activity), uid);
if (acc != null) {
requestImmediateContactsSync(acc);
ContentResolver.setSyncAutomatically(acc, Utils.SYNC_AUTHORITY, true);
TindroidApp.startWatchingContacts(activity, acc);
}
Intent intent = new Intent(activity, ChatsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(intent);
activity.finish();
}
public static void setUpPeriodicSync(Context context, String inputId) {
inputId = inputId==null?"":inputId;
Account account = DummyAccountService.getAccount(context);
AccountManager accountManager =
(AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
if (!accountManager.addAccountExplicitly(account, null, null)) {
Log.w(TAG, "Account already exists.");
// return;
}
Log.d(TAG, "Set sync with "+inputId);
ContentResolver.setIsSyncable(account, CONTENT_AUTHORITY, 1);
ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true);
Bundle bundle = new Bundle();
bundle.putString(SyncAdapter.BUNDLE_KEY_INPUT_ID, inputId);
ContentResolver.addPeriodicSync(account, CONTENT_AUTHORITY, bundle,
SyncAdapter.SYNC_FREQUENCY_SEC/6); //Sync every hour b/c why not?
}
/**
* Add account for Birthday Adapter to Android system
*/
public Bundle addAccountAndSync() {
Log.d(getClass().getSimpleName(), "Adding calendar account : " + account.name);
// enable automatic sync once per day
ContentResolver.setSyncAutomatically(account, authority, true);
ContentResolver.setIsSyncable(account, type, 1);
// add periodic sync interval once per day
long freq = AlarmManager.INTERVAL_DAY;
ContentResolver.addPeriodicSync(account, type, new Bundle(), freq);
AccountManager accountManager = AccountManager.get(context);
if (accountManager.addAccountExplicitly(account, null, null)) {
Bundle result = new Bundle();
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
// Force a sync! Even when background sync is disabled, this will force one sync!
manualSync();
return result;
} else {
return null;
}
}
public boolean requestSync(boolean onlyCheckins) {
if (!isUserLoggedIn()) {
return false;
}
Bundle extras = new Bundle();
extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
extras.putString(SyncAdapter.EXTRA_AUTH_TOKEN, mAuthToken);
extras.putBoolean(SyncAdapter.EXTRA_ONLY_CHECKINS, onlyCheckins);
ContentResolver.setSyncAutomatically(mAccount, Table.AUTHORITY, true);
ContentResolver.setIsSyncable(mAccount, Table.AUTHORITY, 1);
if (ContentResolver.isSyncPending(mAccount, Table.AUTHORITY) ||
ContentResolver.isSyncActive(mAccount, Table.AUTHORITY)) {
ContentResolver.cancelSync(mAccount, Table.AUTHORITY);
}
ContentResolver.requestSync(mAccount, Table.AUTHORITY, extras);
return true;
}
private static void onAccountCreated(Account newAccount, Context context) {
/*
* Since we've created an account
*/
SunshineSyncAdapter.configurePeriodicSync(context, SYNC_INTERVAL, SYNC_FLEXTIME);
/*
* Without calling setSyncAutomatically, our periodic sync will not be enabled.
*/
ContentResolver.setSyncAutomatically(newAccount, context.getString(R.string.content_authority), true);
/*
* Finally, let's do a sync to get things started
*/
syncImmediately(context);
}
private void startSync(Account account) {
final int SYNC_INTERVAL = 3600 * 6;
final String AUTHORITY = mContext.getString(R.string.content_provider_authorities);
//Force request a sync and also enable the auto sync
Bundle bundle = new Bundle();
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
ContentResolver.requestSync(account, AUTHORITY, bundle);
ContentResolver.setSyncAutomatically(account, AUTHORITY, true);
ContentResolver.addPeriodicSync(account, AUTHORITY, Bundle.EMPTY, SYNC_INTERVAL);
}
static synchronized void requestImmediateContactsSync(Account acc) {
Bundle bundle = new Bundle();
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
ContentResolver.requestSync(acc, Utils.SYNC_AUTHORITY, bundle);
ContentResolver.setSyncAutomatically(acc, Utils.SYNC_AUTHORITY, true);
}
/**
* Disables sync for all accounts.
*
* @param context the context
*/
private static void disableSyncForAll(Context context) {
Account[] accounts = AccountManager.get(context).getAccountsByType(Constants.ACCOUNT_TYPE);
for (Account account : accounts) {
ContentResolver.cancelSync(account, SYNC_AUTHORITY);
ContentResolver.setIsSyncable(account, SYNC_AUTHORITY, 0);
ContentResolver.setSyncAutomatically(account, SYNC_AUTHORITY, false);
}
}
public static void upgrade(Context context) {
final int currentVersion = Settings.get(context).getInt(SETTINGS_VERSION, -1);
if (currentVersion == -1) {
Settings.get(context)
.edit()
.putInt(SETTINGS_VERSION, VERSION)
.putInt(Settings.VERSION_CODE, BuildConfig.VERSION_CODE)
.apply();
return;
}
if (currentVersion < VERSION) {
if (currentVersion < 1) {
if (TraktLinkSettings.isLinked(context)) {
Account account = Accounts.getAccount(context);
ContentResolver.removePeriodicSync(account, BuildConfig.PROVIDER_AUTHORITY,
new Bundle());
ContentResolver.setSyncAutomatically(account, BuildConfig.PROVIDER_AUTHORITY, false);
ContentResolver.setIsSyncable(account, BuildConfig.PROVIDER_AUTHORITY, 0);
}
}
if (currentVersion < 2) {
Settings.get(context)
.edit()
.remove("suggestions")
.remove("lastSyncHidden")
.remove("lastPurge")
.remove("tmdbLastConfigurationUpdate")
.apply();
}
if (currentVersion < 3) {
TraktTimestamps.getSettings(context).edit().remove(TraktTimestamps.EPISODE_RATING).apply();
}
if (currentVersion < 4) {
boolean linked = Settings.get(context).getBoolean(TraktLinkSettings.TRAKT_LINKED, false);
if (linked) {
Settings.get(context)
.edit()
.putBoolean(TraktLinkSettings.TRAKT_LINK_PROMPTED, true)
.apply();
}
}
if (currentVersion < 6) {
TraktTimestamps.clear(context);
}
Settings.get(context).edit().putInt(SETTINGS_VERSION, VERSION).apply();
}
Settings.get(context).edit().putInt(Settings.VERSION_CODE, BuildConfig.VERSION_CODE).apply();
}
@Override
public void setSyncAutomatically(Account account, String authority, boolean sync) {
ContentResolver.setSyncAutomatically(account, authority, sync);
}
public static void requestManualSync(Context context, Bundle args) {
Account account = AccountUtils.getActiveAccount(context);
if (account != null) {
LOGD(TAG, "Requesting manual sync for account " + account.name
+" args=" + args.toString());
args.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
args.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
args.putBoolean(SyncAdapter.EXTRA_SYNC_REMOTE, false);
AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
accountManager.addAccountExplicitly(account, null, null);
// Inform the system that this account is eligible for auto sync when the network is up
ContentResolver.setSyncAutomatically(account, V2exContract.CONTENT_AUTHORITY, true);
// Inform the system that this account supports sync
ContentResolver.setIsSyncable(account, V2exContract.CONTENT_AUTHORITY, 1);
boolean pending = ContentResolver.isSyncPending(account,
V2exContract.CONTENT_AUTHORITY);
if (pending) {
LOGD(TAG, "Warning: sync is PENDING. Will cancel.");
}
boolean active = ContentResolver.isSyncActive(account,
V2exContract.CONTENT_AUTHORITY);
if (active) {
LOGD(TAG, "Warning: sync is ACTIVE. Will cancel.");
}
if (pending || active) {
LOGD(TAG, "Cancelling previously pending/active sync.");
ContentResolver.cancelSync(account, V2exContract.CONTENT_AUTHORITY);
}
LOGD(TAG, "Requesting sync now.");
ContentResolver.requestSync(account, V2exContract.CONTENT_AUTHORITY, args);
} else {
LOGD(TAG, "Can't request manual sync -- no chosen account.");
}
}
private void finishLogin(Intent intent) {
Log.d("aptoide", TAG + "> finishLogin");
String accountName = intent.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
String accountPassword = intent.getStringExtra(PARAM_USER_PASS);
final String accountType = intent.hasExtra(ARG_ACCOUNT_TYPE)
? intent.getStringExtra(ARG_ACCOUNT_TYPE)
: Aptoide.getConfiguration().getAccountType();
final Account account = new Account(accountName, accountType);
if (getIntent().getBooleanExtra(ARG_IS_ADDING_NEW_ACCOUNT, false)) {
Log.d("aptoide", TAG + "> finishLogin > addAccountExplicitly");
String authtoken = intent.getStringExtra(AccountManager.KEY_AUTHTOKEN);
String authtokenType = mAuthTokenType;
// Creating the account on the device and setting the auth token we got
// (Not setting the auth token will cause another call to the server to authenticate the user)
mAccountManager.addAccountExplicitly(account, accountPassword, null);
mAccountManager.setAuthToken(account, authtokenType, authtoken);
} else {
Log.d("aptoide", TAG + "> finishLogin > setPassword");
mAccountManager.setPassword(account, accountPassword);
}
setAccountAuthenticatorResult(intent.getExtras());
setResult(RESULT_OK, intent);
if (fromPreviousAptoideVersion) {
PreferenceManager.getDefaultSharedPreferences(this).edit().remove(Constants.LOGIN_USER_LOGIN).commit();
}
finish();
if (registerDevice != null && registerDevice.isChecked() && hasQueue)
startService(new Intent(this, RabbitMqService.class));
ContentResolver.setSyncAutomatically(account, Aptoide.getConfiguration().getUpdatesSyncAdapterAuthority(), true);
if (Build.VERSION.SDK_INT >= 8)
ContentResolver.addPeriodicSync(account, Aptoide.getConfiguration().getUpdatesSyncAdapterAuthority(), new Bundle(), 43200);
ContentResolver.setSyncAutomatically(account, Aptoide.getConfiguration().getAutoUpdatesSyncAdapterAuthority(), true);
}
private static void onAccountCreated(Account newAccount, Context context) {
configurePeriodicSync(context, SYNC_INTERVAL, SYNC_FLEXTIME);
ContentResolver.setSyncAutomatically(newAccount,
context.getString(R.string.sync_authority), true);
syncImmediately(context);
}
public void turnOffSync(Account mAccount) {
ContentResolver.setSyncAutomatically(mAccount, AUTHORITY, false);
}
private static void onAccountCreated(Account newAccount, Context context) {
WoodminSyncAdapter.configurePeriodicSync(context, SYNC_INTERVAL, SYNC_FLEXTIME);
ContentResolver.setSyncAutomatically(newAccount, context.getString(R.string.content_authority), true);
syncImmediately(context);
}
static void enableSync(Context context) {
setComponentEnabled(context, EarthsSyncService.class, true);
final AccountManager am = AccountManager.get(context);
final Account account = makeAccount(context);
am.addAccountExplicitly(account, null, Bundle.EMPTY);
ContentResolver.setIsSyncable(account, EarthsContract.AUTHORITY, 1);
ContentResolver.addPeriodicSync(account, EarthsContract.AUTHORITY,
Bundle.EMPTY, TimeUnit.MINUTES.toSeconds(10));
ContentResolver.setSyncAutomatically(account, EarthsContract.AUTHORITY, true);
}