Example usage for android.content ContentResolver addPeriodicSync

List of usage examples for android.content ContentResolver addPeriodicSync

Introduction

In this page you can find the example usage for android.content ContentResolver addPeriodicSync.

Prototype

public static void addPeriodicSync(Account account, String authority, Bundle extras, long pollFrequency) 

Source Link

Document

Specifies that a sync should be requested with the specified the account, authority, and extras at the given frequency.

Usage

From source file:saschpe.birthdays.helper.AccountHelper.java

public static Bundle addAccount(Context context) {
    Log.d(TAG, "AccountHelper.addAccount: Adding account...");

    final Account account = new Account(context.getString(R.string.app_name),
            context.getString(R.string.account_type));
    AccountManager manager = AccountManager.get(context);

    if (manager.addAccountExplicitly(account, null, null)) {
        // Enable automatic sync once per day
        ContentResolver.setSyncAutomatically(account, context.getString(R.string.content_authority), true);
        ContentResolver.setIsSyncable(account, context.getString(R.string.content_authority), 1);

        // Add periodic sync interval based on user preference
        final long freq = PreferencesHelper.getPeriodicSyncFrequency(context);
        ContentResolver.addPeriodicSync(account, context.getString(R.string.content_authority), new Bundle(),
                freq);/* w ww .  ja v a2s  .c o m*/

        Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        Log.i(TAG, "Account added: " + account.name);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            manager.notifyAccountAuthenticated(account);
        }
        return result;
    } else {
        Log.e(TAG, "Adding account explicitly failed!");
        return null;
    }
}

From source file:inforuh.eventfinder.sync.SyncAdapter.java

public static void periodicSync(Context context, int interval, int flexTime) {
    Account account = getAccount(context);
    String authority = context.getString(R.string.content_authority);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        SyncRequest request = new SyncRequest.Builder().syncPeriodic(interval, flexTime)
                .setSyncAdapter(account, authority).setExtras(new Bundle()).build();
        ContentResolver.requestSync(request);
    } else {/*from  w  ww.  j a v  a2s.  c o m*/
        ContentResolver.addPeriodicSync(account, authority, Bundle.EMPTY, interval);
    }
}

From source file:org.birthdayadapter.util.AccountHelper.java

/**
 * Add account for Birthday Adapter to Android system
 *//* w w  w  .  jav  a 2  s .c om*/
public Bundle addAccountAndSync() {
    Log.d(Constants.TAG, "Adding account...");

    // enable automatic sync once per day
    ContentResolver.setSyncAutomatically(Constants.ACCOUNT, Constants.CONTENT_AUTHORITY, true);
    ContentResolver.setIsSyncable(Constants.ACCOUNT, Constants.ACCOUNT_TYPE, 1);

    // add periodic sync interval once per day
    long freq = AlarmManager.INTERVAL_DAY;
    ContentResolver.addPeriodicSync(Constants.ACCOUNT, Constants.ACCOUNT_TYPE, new Bundle(), freq);

    AccountManager am = AccountManager.get(mContext);
    if (am.addAccountExplicitly(Constants.ACCOUNT, null, null)) {
        Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, Constants.ACCOUNT.name);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT.type);

        // Force a sync! Even when background sync is disabled, this will force one sync!
        manualSync();

        return result;
    } else {
        return null;
    }
}

From source file:com.ntsync.android.sync.shared.SyncUtils.java

/**
 * /*  ww w  .  ja  v a  2  s  . co  m*/
 * @param authority
 *            the provider to specify in the sync request
 * @param extras
 *            extra parameters to go along with the sync request
 * @param frequency
 *            in seconds
 * @param context
 */
public static void addPeriodicSync(String authority, Bundle extras, long frequency, Context context) {

    AccountManager am = AccountManager.get(context);
    Account[] accounts = am.getAccountsByType(Constants.ACCOUNT_TYPE);
    for (Account ac : accounts) {
        ContentResolver.addPeriodicSync(ac, authority, extras, frequency);
    }
}

From source file:tech.salroid.filmy.syncs.FilmySyncAdapter.java

/**
 * Helper method to schedule the sync adapter periodic execution
 *///from  w  ww .  j a v  a 2s  .  c o m
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);
    }
}

From source file:com.creationgroundmedia.popularmovies.sync.MovieSyncAdapter.java

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 {/*  ww w. j av  a 2  s  . com*/
        ContentResolver.addPeriodicSync(account, authority, new Bundle(), syncInterval);
    }
}

From source file:com.rowland.hashtrace.sync.TweetHashTracerSyncAdapter.java

/**
 * Helper method to schedule the sync adapter periodic execution
 *//*from   w  ww  .j  a va 2s  .c  om*/
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).build();
        ContentResolver.requestSync(request);
    } else {
        ContentResolver.addPeriodicSync(account, authority, new Bundle(), syncInterval);
    }
}

From source file:com.katamaditya.apps.weather4u.weathersync.Weather4USyncAdapter.java

/**
 * Helper method to schedule the sync adapter periodic execution
 *///  w w w. j a v  a 2s  .  c o m
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);
    }
}

From source file:com.stoneapp.ourvlemoodle2.activities.MainActivity.java

/**
 * Create an entry for this application in the system account list, if it isn't already there.
 *
 * @param context Context//  w  w  w. j  a va  2  s  .  com
 */
public static void CreateSyncAccount(Context context) {
    final String AUTHORITY = BuildConfig.APPLICATION_ID + ".provider";
    // Value below must match the account type specified in res/xml/syncadapter.xml
    final String ACCOUNT_TYPE = BuildConfig.APPLICATION_ID;
    // hours in seconds
    final long SYNC_INTERVAL = SettingsUtils.getSyncInterval(context) * 60L * 60L;

    List<MoodleSiteInfo> sites = MoodleSiteInfo.listAll(MoodleSiteInfo.class);
    String accountName = sites.get(0).getUsername();

    if (TextUtils.isEmpty(accountName))
        accountName = "OurVLE User";

    // Create account, if it's missing. (Either first run, or user has deleted account.)
    Account account = new Account(accountName, ACCOUNT_TYPE);

    AccountManager accountManager = (AccountManager) context.getSystemService(ACCOUNT_SERVICE);

    /*
     * Add the account and account type, no password or user data
     * If successful, return the Account object, otherwise report an error.
     */
    if (accountManager.addAccountExplicitly(account, null, null)) {
        // Inform the system that this account is eligible for auto sync when the network is up
        ContentResolver.setSyncAutomatically(account, AUTHORITY, true);
        // Recommend a schedule for automatic synchronization. The system may modify this based
        // on other scheduled syncs and network utilization.
        ContentResolver.addPeriodicSync(account, AUTHORITY, Bundle.EMPTY, SYNC_INTERVAL);
    }
}

From source file:com.grepsound.activities.MainActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);//ww w.j ava 2s .com

    mDarkHoverView = findViewById(R.id.dark_hover_view);
    mDarkHoverView.setAlpha(0);

    getFragmentManager().addOnBackStackChangedListener(this);

    fMenu = new PlayerFragment();
    mMainFrag = new MyProfileFragment();
    getFragmentManager().beginTransaction().replace(R.id.move_to_back_container, mMainFrag)
            .replace(R.id.left_drawer, fMenu).commit();
    int layoutSizeMask = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;

    mDrawerIsLocked = (layoutSizeMask == Configuration.SCREENLAYOUT_SIZE_LARGE
            || layoutSizeMask == Configuration.SCREENLAYOUT_SIZE_XLARGE)
            && getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;

    setupNavigationDrawer(mDrawerIsLocked);
    mAccount = CreateSyncAccount(this);
    ContentResolver.addPeriodicSync(mAccount, AUTHORITY, new Bundle(), SYNC_INTERVAL);

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    Log.i(TAG, "wifi only is " + prefs.getBoolean("wifi_only", false));

}