Example usage for android.content ContentResolver setSyncAutomatically

List of usage examples for android.content ContentResolver setSyncAutomatically

Introduction

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

Prototype

public static void setSyncAutomatically(Account account, String authority, boolean sync) 

Source Link

Document

Set whether or not the provider is synced when it receives a network tickle.

Usage

From source file:Main.java

public static void setServiceSync(String Authority, boolean on, android.accounts.Account account) {
    ContentResolver.setSyncAutomatically(account, Authority, on);
    if (on)/*  w w w  .j  a  va2  s.  co m*/
        ContentResolver.requestSync(account, Authority, new Bundle());
}

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);//  ww w .  j  av  a 2s  . c  om

        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:org.birthdayadapter.util.AccountHelper.java

/**
 * Add account for Birthday Adapter to Android system
 *///from w w w  . jav  a 2  s .  com
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.creationgroundmedia.popularmovies.sync.MovieSyncAdapter.java

private static void onAccountCreated(Account newAccount, Context context) {
    /*//from w  w  w.java 2s  .  c  o m
     * Since we've created an account
     */
    Log.d(LOG_TAG, "onAccountCreated called");
    MovieSyncAdapter.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);
}

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

private static void onAccountCreated(Account acc, Context context) {
    Log.d(LOG_TAG, "Sync account created");
    SyncAdapter.periodicSync(context, SYNC_INTERVAL, SYNC_FLEXTIME);
    ContentResolver.setSyncAutomatically(acc, context.getString(R.string.content_authority), true);
    startSync(context);/*from   ww w .  j  a v  a 2s . c o m*/
}

From source file:at.bitfire.davdroid.syncadapter.AccountDetailsFragment.java

void addAccount() {
    ServerInfo serverInfo = (ServerInfo) getArguments().getSerializable(KEY_SERVER_INFO);
    String accountName = editAccountName.getText().toString();

    AccountManager accountManager = AccountManager.get(getActivity());
    Account account = new Account(accountName, Constants.ACCOUNT_TYPE);
    Bundle userData = new Bundle();
    userData.putString(Constants.ACCOUNT_KEY_BASE_URL, serverInfo.getBaseURL());
    userData.putString(Constants.ACCOUNT_KEY_USERNAME, serverInfo.getUserName());
    userData.putString(Constants.ACCOUNT_KEY_AUTH_PREEMPTIVE, Boolean.toString(serverInfo.isAuthPreemptive()));

    boolean syncContacts = false;
    for (ServerInfo.ResourceInfo addressBook : serverInfo.getAddressBooks())
        if (addressBook.isEnabled()) {
            userData.putString(Constants.ACCOUNT_KEY_ADDRESSBOOK_PATH, addressBook.getPath());
            ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
            syncContacts = true;/*  w w  w . j  ava 2  s. c o  m*/
            continue;
        }
    if (syncContacts) {
        ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
        ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);
    } else
        ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 0);

    if (accountManager.addAccountExplicitly(account, serverInfo.getPassword(), userData)) {
        // account created, now create calendars
        ContentResolver.setIsSyncable(account, "com.android.calendar", 0);

        getActivity().finish();
    } else
        Toast.makeText(getActivity(), "Couldn't create account (account with this name already existing?)",
                Toast.LENGTH_LONG).show();
}

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/*from  w  w  w. j a va  2  s  .  co m*/
 */
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:org.enbyted.android.zseinfo.view.activity.MainActivity.java

public static Account createSyncAccount(Context context) {
    Account newAccount = new Account(ACCOUNT, ACCOUNT_TYPE);
    AccountManager accountManager = (AccountManager) context.getSystemService(ACCOUNT_SERVICE);

    if (accountManager.addAccountExplicitly(newAccount, null, null)) {
        ContentResolver.setSyncAutomatically(newAccount, AUTHORITY, true);
        ContentResolver.setIsSyncable(newAccount, AUTHORITY, 1);
    } else {//from  w w  w . j  a  v a  2s  . c om

    }
    return newAccount;
}

From source file:dev.drsoran.moloko.auth.AuthenticatorActivity.java

/**
 * Response is received from the server for authentication request. Sets the AccountAuthenticatorResult which is sent
 * back to the caller. Also sets the authToken in AccountManager for this account.
 *///from  w  w w. jav  a 2s.  c  o m
@Override
public void onAuthenticationFinished(RtmAuth rtmAuth) {
    final Account account = new Account(rtmAuth.getUser().getUsername(), Constants.ACCOUNT_TYPE);
    try {
        boolean ok = true;

        if (isNewAccount) {
            ok = accountManager.addAccountExplicitly(account, rtmAuth.getToken(), null);
            if (ok) {
                ContentResolver.setSyncAutomatically(account, Rtm.AUTHORITY, true);
            }
        }

        if (ok) {
            accountManager.setUserData(account, Constants.FEAT_API_KEY, MolokoApp.getRtmApiKey(this));
            accountManager.setUserData(account, Constants.FEAT_SHARED_SECRET,
                    MolokoApp.getRtmSharedSecret(this));
            accountManager.setUserData(account, Constants.FEAT_PERMISSION, rtmAuth.getPerms().toString());
            accountManager.setUserData(account, Constants.ACCOUNT_USER_ID, rtmAuth.getUser().getId());
            accountManager.setUserData(account, Constants.ACCOUNT_FULLNAME, rtmAuth.getUser().getFullname());

            final Intent intent = new Intent();

            intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, rtmAuth.getUser().getUsername());
            // We store the authToken as password
            intent.putExtra(AccountManager.KEY_PASSWORD, rtmAuth.getToken());
            intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
            intent.putExtra(AccountManager.KEY_AUTHTOKEN, rtmAuth.getToken());

            intent.putExtra(AccountManager.KEY_BOOLEAN_RESULT, true);

            setAccountAuthenticatorResult(intent.getExtras());
            setResult(RESULT_OK, intent);
        }
    } catch (SecurityException e) {
        MolokoApp.Log.e(getClass(), e.getLocalizedMessage());
        onAuthenticationFailed(getString(R.string.auth_err_cause_scurity));
    } finally {
        finish();
    }
}

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

private static void onAccountCreated(Account newAccount, Context context) {

    /*//from  w  w w .ja v a 2s.  c o  m
     * Since we've created an account
     */
    FilmySyncAdapter.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);
}