下面列出了android.content.ContentResolver#requestSync ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Helper method to trigger an immediate sync ("refreshAccounts").
* <p>
* <p>This should only be used when we need to preempt the normal sync schedule. Typically, this
* means the user has pressed the "refreshAccounts" button.
* <p>
* Note that SYNC_EXTRAS_MANUAL will cause an immediate sync, without any optimization to
* preserve battery life. If you know new data is available (perhaps via a GCM notification),
* but the user is not actively waiting for that data, you should omit this flag; this will give
* the OS additional freedom in scheduling your sync request.
*/
public void triggerRefresh(MovirtAccount account) {
Account acc = account.getAccount();
Bundle b = new Bundle();
// Disable sync backoff and ignore sync preferences. In other words...perform sync NOW!
b.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
b.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
// cancel sync because account will not sync if similar sync is already in progress
if (ContentResolver.isSyncPending(acc, OVirtContract.CONTENT_AUTHORITY) ||
ContentResolver.isSyncActive(acc, OVirtContract.CONTENT_AUTHORITY)) {
ContentResolver.cancelSync(acc, OVirtContract.CONTENT_AUTHORITY);
}
ContentResolver.requestSync(acc, OVirtContract.CONTENT_AUTHORITY, b);
}
/**
* Helper method to schedule the sync adapter periodic execution
*/
public static void configurePeriodicSync(Context context, int syncInterval, int flexTime) {
Account account = getSyncAccount(context);
String authority = context.getString(R.string.content_authority);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// we can enable inexact timers in our periodic sync
SyncRequest request = new SyncRequest.Builder().
syncPeriodic(syncInterval, flexTime).
setSyncAdapter(account, authority).
setExtras(new Bundle()).build();
ContentResolver.requestSync(request);
} else {
ContentResolver.addPeriodicSync(account,
authority, new Bundle(), syncInterval);
}
}
public static void syncImmediately(Context context, SwipeRefreshLayout refreshLayout)
{
sSwipeContainer = refreshLayout;
if (!isNetworkAvailable(context))
{
setSwipeRefreshing(false);
Log.d(LOG_TAG, "No internet available");
return;
}
Bundle bundle = new Bundle();
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
ContentResolver.requestSync(getSyncAccount(context), DatabaseContract.CONTENT_AUTHORITY, bundle);
}
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences prefs = context.getSharedPreferences(context.getPackageName() + "_preferences", Context.MODE_MULTI_PROCESS);
boolean wifiOnly = prefs.getBoolean("wifi_only", false);
boolean chargingOnly = prefs.getBoolean("charging_only", false);
String action = intent.getAction();
if((wifiOnly && DeviceUtil.isWifi(context) && action.equals("android.net.wifi.STATE_CHANGE")) || (chargingOnly && action.equals("android.intent.action.ACTION_POWER_CONNECTED"))){
AccountManager am = AccountManager.get(context);
Account[] accs = am.getAccountsByType(context.getString(R.string.ACCOUNT_TYPE));
if (accs.length > 0){
Account account = accs[0];
SharedPreferences.Editor editor = prefs.edit();
if (prefs.getBoolean("missed_contact_sync", false)){
ContentResolver.requestSync(account, ContactsContract.AUTHORITY, new Bundle());
editor.putBoolean("missed_contact_sync", false);
}
if (prefs.getBoolean("missed_calendar_sync", false)){
ContentResolver.requestSync(account, CalendarContract.AUTHORITY, new Bundle());
editor.putBoolean("missed_calendar_sync", false);
}
editor.commit();
}
}
}
private void initiateSync() {
Log.d(TAG, "initiateSync... google account:"+ mGoogleAccountName);
Account account = getGoogleAccount(mGoogleAccountName);
if (account != null) {
Log.d(TAG, "Found account! init sync...");
// Pass the settings flags by inserting them in a bundle
Bundle settingsBundle = new Bundle();
settingsBundle.putBoolean(
ContentResolver.SYNC_EXTRAS_MANUAL, true);
settingsBundle.putBoolean(
ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
/*
* Request the sync for the default account, authority, and
* manual sync settings
*/
ContentResolver.requestSync(account, TaskSyncContentProvider.AUTHORITY, settingsBundle);
}
}
/**
* Helper method to schedule the sync adapter periodic execution
*/
public static void configurePeriodicSync(Context context, int syncInterval, int flexTime) {
Account account = getSyncAccount(context);
String authority = context.getString(R.string.content_authority);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// we can enable inexact timers in our periodic sync
SyncRequest request = new SyncRequest.Builder().
syncPeriodic(syncInterval, flexTime).
setSyncAdapter(account, authority).
setExtras(new Bundle()).build();
ContentResolver.requestSync(request);
} else {
ContentResolver.addPeriodicSync(account,
authority, new Bundle(), syncInterval);
}
}
private void requestSync(AuthorityInfo authorityInfo, int reason, Bundle extras,
@SyncExemption int syncExemptionFlag) {
if (android.os.Process.myUid() == android.os.Process.SYSTEM_UID
&& mSyncRequestListener != null) {
mSyncRequestListener.onSyncRequest(authorityInfo.target, reason, extras,
syncExemptionFlag);
} else {
SyncRequest.Builder req =
new SyncRequest.Builder()
.syncOnce()
.setExtras(extras);
req.setSyncAdapter(authorityInfo.target.account, authorityInfo.target.provider);
ContentResolver.requestSync(req.build());
}
}
private void requestSync(Account account, int userId, int reason, String authority,
Bundle extras, @SyncExemption int syncExemptionFlag) {
// If this is happening in the system process, then call the syncrequest listener
// to make a request back to the SyncManager directly.
// If this is probably a test instance, then call back through the ContentResolver
// which will know which userId to apply based on the Binder id.
if (android.os.Process.myUid() == android.os.Process.SYSTEM_UID
&& mSyncRequestListener != null) {
mSyncRequestListener.onSyncRequest(
new EndPoint(account, authority, userId),
reason, extras, syncExemptionFlag);
} else {
ContentResolver.requestSync(account, authority, extras);
}
}
public static void syncImmediately(Context context)
{
Bundle bundle = new Bundle();
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
ContentResolver.requestSync(getSyncAccount(context), DatabaseContract.CONTENT_AUTHORITY, bundle);
}
/**
* Helper method to have the sync adapter sync immediately
* @param context The context used to access the account service
*/
public static void syncImmediately(Context context) {
Bundle bundle = new Bundle();
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
ContentResolver.requestSync(getSyncAccount(context),
context.getString(R.string.content_authority), bundle);
}
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);
}
public static void syncImmediately(final Context context) {
Bundle bundle = new Bundle();
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
ContentResolver.requestSync(getSyncAccount(context), context.getString(R.string.content_authority), bundle);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sync:
for (Account account : mAccounts) {
Bundle settingsBundle = new Bundle();
settingsBundle.putBoolean(
ContentResolver.SYNC_EXTRAS_MANUAL, true);
settingsBundle.putBoolean(
ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
ContentResolver.requestSync(account, AUTHORITY, settingsBundle);
}
updateInfo();
break;
case R.id.new_layer:
if (getActivity() != null) {
// View view = getActivity().getWindow().getDecorView().findViewById(android.R.id.content);
View view = getActivity().findViewById(R.id.new_layer);
PopupMenu popup = new PopupMenu(getActivity(), view);
UiUtil.setForceShowIcon(popup);
popup.getMenuInflater().inflate(R.menu.add_layer, popup.getMenu());
popup.setOnMenuItemClickListener(this);
if (!AccountUtil.isProUser(getActivity())) {
popup.getMenu().findItem(R.id.menu_add_ngw).setIcon(R.drawable.ic_lock_black_24dp);
}
popup.show();
}
break;
}
}
/** Ask the SyncAdapter to do its work.
* We call this when we think it's likely that some work is necessary.
*/
private static void requestSync() {
final Bundle extras = new Bundle();
extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
//Ask the framework to run our SyncAdapter.
//We call this far too often,
//but we trust the SyncAdapter system to not actually do the sync too often.
//That seems to work fine as long as the SyncAdapter is in its own process.
//See android:process=":sync" in AndroidManifest.xml
ContentResolver.requestSync(null, Item.AUTHORITY, extras);
}
/**
* Calls {@link ContentResolver#requestSync(Account, String, Bundle)} to trigger a sync. Split
* into a separate method so that it can be overriden in tests.
*/
@VisibleForTesting
void requestSyncFromContentResolver(
Bundle bundle, Account account, String contractAuthority) {
Log.d(TAG, "Request sync: " + account + " / " + contractAuthority + " / "
+ bundle.keySet());
ContentResolver.requestSync(account, contractAuthority, bundle);
}
public static void requestSync(Context mContext, String inputId) {
inputId = inputId==null?"":inputId;
Bundle bundle = new Bundle();
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
bundle.putString(SyncAdapter.BUNDLE_KEY_INPUT_ID, inputId);
Log.d(TAG, "Request sync");
ContentResolver.requestSync(DummyAccountService.getAccount(mContext), CONTENT_AUTHORITY,
bundle);
}
public void requestSync(String authority, Bundle bundle) {
Account account = mUser.getAccount();
Bundle settingsBundle = new Bundle();
settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
if (bundle != null) {
settingsBundle.putAll(bundle);
}
ContentResolver.requestSync(account, authority, settingsBundle);
}
public void requestCommentsSync(Item item, int limit) {
Bundle settingsBundle = makeExpeditedManualSyncSettingsBundle();
settingsBundle.putInt(HackerNewsSyncAdapter.EXTRA_STORY, item.getId());
settingsBundle.putInt(HackerNewsSyncAdapter.EXTRA_KEY_LIMIT, limit);
ContentResolver.requestSync(mAccount, HackerNewsData.CONTENT_AUTHORITY, settingsBundle);
}
public static void requestCalendarSync(Context context) {
Account account = getAccount(context);
ContentResolver.requestSync(account, BuildConfig.AUTHORITY_DUMMY_CALENDAR, new Bundle());
}
/**
* Helper method to trigger an immediate sync ("refresh").
*
* <p>This should only be used when we need to preempt the normal sync schedule. Typically, this
* means the user has pressed the "refresh" button.
*
* Note that SYNC_EXTRAS_MANUAL will cause an immediate sync, without any optimization to
* preserve battery life. If you know new data is available (perhaps via a GCM notification),
* but the user is not actively waiting for that data, you should omit this flag; this will give
* the OS additional freedom in scheduling your sync request.
*/
public static void TriggerRefresh() {
Bundle b = new Bundle();
// Disable sync backoff and ignore sync preferences. In other words...perform sync NOW!
b.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
b.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
ContentResolver.requestSync(
GenericAccountService.GetAccount(ACCOUNT_TYPE), // Sync account
FeedContract.CONTENT_AUTHORITY, // Content authority
b); // Extras
}