gpt4 book ai didi

android - Syncadapter onPerformSync 第一次被调用两次

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:36:19 24 4
gpt4 key购买 nike

除了一件事,我的 syncadapter 运行良好。用户安装应用程序后,我的应用程序会同步两次。稍后,如果我在“设置”中手动同步它,它只会按预期同步一次。这只是发生这种情况的应用程序的第一次运行。

这是我的“onCreate”中的代码,它创建帐户(如果尚未创建)并设置 syncadapter。对我做错了什么有什么想法吗?

    if (accountManager.addAccountExplicitly(appAccount, null, null)) {
ContentResolver.setIsSyncable(appAccount, PROVIDER, 1);
ContentResolver.setSyncAutomatically(appAccount, PROVIDER, true);

Bundle extras = new Bundle();
extras.putBoolean("dummy stuff", true);
ContentResolver.addPeriodicSync(appAccount, PROVIDER, extras, 43200);
}

我希望应用程序在安装后立即同步一次,然后根据“addPeriodicSync”语句定期同步。

最佳答案

我也观察到了这种行为。

正确的是,addAccountExplicit() 将触发过时帐户的系统范围帐户重新同步。

澄清

但是,Zapek 关于 addPeriodic sync 或 request sync 是“立即”同步的观察并不完全正确。两者都在排队。此外,以下内容适用于 addPeriodicSync():

These periodic syncs honor the "syncAutomatically" and "masterSyncAutomatically" settings. Although these sync are scheduled at the specified frequency, it may take longer for it to actually be started if other syncs are ahead of it in the sync operation queue. This means that the actual start time may drift. (Documentation)

关于你的问题

运行同步适配器的培训中描述了您的体验:

The method addPeriodicSync() doesn't disable setSyncAutomatically(), so you may get multiple sync runs in a relatively short period of time. Also, only a few sync adapter control flags are allowed in a call to addPeriodicSync(); the flags that are not allowed are described in the referenced documentation for addPeriodicSync(). Android Training Sync Adapter

Google 自己的解决方案看起来像你的,频率甚至更低 (60*60=3600):

    if (accountManager.addAccountExplicitly(account, null, null)) {
// Inform the system that this account supports sync
ContentResolver.setIsSyncable(account, CONTENT_AUTHORITY, 1);
// Inform the system that this account is eligible for auto sync when the network is up
ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true);
// Recommend a schedule for automatic synchronization. The system may modify this based
// on other scheduled syncs and network utilization.
ContentResolver.addPeriodicSync(
account, CONTENT_AUTHORITY, new Bundle(),SYNC_FREQUENCY);
newAccount = true;
}

命题

我建议使用 onPerformSync() 中的 SyncStats 将有关初始同步的一些信息实际返回给系统,以便它可以更有效地进行调度。

syncResult.stats.numEntries++; // For every dataset

如果已经安排了其他任务 - 调查,这可能无济于事

另外,可以设置一个标志“isInitialOnPerformSync”(w.sharedPreferences),以导致其他任务备份。

syncResult.delayUntil = <time>;

我个人不太喜欢在初始同步后创建固定的不同步时间范围。

进一步的考虑 - 立即初始同步

如说明中所述,同步不会根据您的设置立即运行。有一个解决方案,可以让您立即同步。这不会影响同步设置,也不会导致它们退避,这就是为什么这不能解决您的问题,但它的效果是您的用户不必等待同步开始。如果您使用此功能,则很重要以这种方式显示应用中的主要内容。

代码:在您的正常应用进程中为 isInitialSync 设置一个标志(您将其保存在例如 defaultSharedPreferences 中)。您甚至可以使用在首次完成安装或登录(如果需要身份验证)后,您可以调用立即同步,如下所示。

/**
* Start an asynchronous sync operation immediately. </br>
*
* @param account
*/
public static void requestSyncImmediately(Account account) {
// Disable sync backoff and ignore sync preferences. In other words...perform sync NOW!
Bundle settingsBundle = new Bundle();
settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
// Request sync with settings
ContentResolver.requestSync(account, SyncConstants.CONTENT_AUTHORITY, settingsBundle);
}

关于android - Syncadapter onPerformSync 第一次被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12044152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com