如何为同步适配器设置15分钟的同步间隔。

时间:2017-06-06 08:23:52

标签: java android

以下是更新后的代码

public class MainActivityNew extends AppCompatActivity  {

    private static final String Tag = "MainActivity";
    // Instance fields
    Account mAccount;

    public static final long SECONDS_PER_MINUTE = 60L;
    public static final long SYNC_INTERVAL_IN_MINUTES = 15L;
    public static final long SYNC_INTERVAL =
            SYNC_INTERVAL_IN_MINUTES *
                    SECONDS_PER_MINUTE;

    private static final int NOTIFICATION_EX = 1;

    ContentResolver resolver;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
       // Create the dummy account
        mAccount = CreateSyncAccount(this);
        resolver = getContentResolver();
        resolver.setIsSyncable(mAccount, CommonUtilities.AUTHORITY, 1);
        resolver.setSyncAutomatically(mAccount, CommonUtilities.AUTHORITY, true);
        resolver.addPeriodicSync(
                mAccount,
                CommonUtilities.AUTHORITY,
                Bundle.EMPTY,
                SYNC_INTERVAL);}

    public static Account CreateSyncAccount(Context context) {
        // Create the mAccount call_type and default mAccount
        Account newAccount = new Account(
                ACCOUNT, ACCOUNT_TYPE);
        // Get an instance of the Android mAccount manager
        AccountManager accountManager =
                (AccountManager) context.getSystemService(
                        ACCOUNT_SERVICE);
        /*
         * Add the mAccount and mAccount call_type, no password or user data
         * If successful, return the Account object, otherwise report an error.
         */
        if (accountManager.addAccountExplicitly(newAccount, null, null)) {
            /*
             * If you don't set android:syncable="true" in
             * in your <provider> element in the manifest,
             * then call context.setIsSyncable(mAccount, AUTHORITY, 1)
             * here.
             */
            ContentResolver.setIsSyncable(newAccount, AUTHORITY, 1);
            ContentResolver.setMasterSyncAutomatically(true);
            ContentResolver.setSyncAutomatically(newAccount, AUTHORITY, true);
        } else {
            /*
             * The mAccount exists or some other error occurred. Log this, report it,
             * or handle it internally.
             */
            Log.d(Tag, "Error occured in creating mAccount");
        }
        return newAccount;
    }

我也发送同步间隔时间,如1 * 60 * 1000

  1. 如何为同步适配器设置15分钟的同步间隔?
  2. 1 * 60 * 1000L和1 * 60 * 1000之间有什么区别?

1 个答案:

答案 0 :(得分:1)

1 * 60 * 1000 = 60 000 = 1分钟,

  1. 使用15 * 60 * 1000将同步间隔设置为15分钟。
  2. 1 * 60 * 1000L返回一个long,其中1 * 60 * 1000返回一个int(不适合大数字)