android – 计划的同步适配器每30秒运行一次

我在应用程序中使用同步适配器定期同步服务器的更改.无论我在pollFrequency中放入什么值,同步每30秒运行一次.

我检查了论坛并尝试了回复中建议的更改,当我在ContentResolver上引发notifyChange时,我将’false’作为syncToNetwork参数传递.

在详细再次进行训练时,我偶然发现了这种差异.

在Google开发者网站上 – >训练部门Sync adapters training
我看到了addPeriodicSync – > pollFrequency参数以毫秒为单位传递

public class MainActivity extends FragmentActivity {
...
// Constants
// Content provider authority
public static final String AUTHORITY = "com.example.android.datasync.provider";

// Account
public static final String ACCOUNT = "default_account";

// Sync interval constants
public static final long MILLISECONDS_PER_SECOND = 1000L;
public static final long SECONDS_PER_MINUTE = 60L;
public static final long SYNC_INTERVAL_IN_MINUTES = 60L;

//This is the line I'm referring to
public static final long SYNC_INTERVAL = SYNC_INTERVAL_IN_MINUTES * 
                                         SECONDS_PER_MINUTE *
                                         MILLISECONDS_PER_SECOND;

// Global variables
// A content resolver for accessing the provider
ContentResolver mResolver;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    // Get the content resolver for your app
    mResolver = getContentResolver();
    /*
     * Turn on periodic syncing
     */
    ContentResolver.addPeriodicSync(
            ACCOUNT,
            AUTHORITY,
            null,
            SYNC_INTERVAL);
    ...
    }
    ...
}

在API参考API Reference上,提到pollFrequency是以秒为单位.什么是预期的单位pollFrequency,毫秒或秒?
任何帮助表示赞赏.

最佳答案 你应该添加

        ContentResolver.setMasterSyncAutomatically(真);你的代码.如果没有这个,setSyncAutomatically将被忽略.

阅读文档here.

点赞