- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
除了一件事,我的 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/
我在 AbstractThreadedSyncAdapter 的 onPerformSync 方法中进行网络操作。我想知道当我的应用程序在前台、后台和死机中运行时,这些操作是否默认在单独的线程上运行。
我的应用程序有一个按照 Android developer site 显示的结构创建的同步适配器. onPerformSync() 方法从互联网获取数据并使用批量插入将其插入数据库: Vector c
我为我的应用构建了一个 SyncAdapter,以便我可以使用 Google Cloud Messaging 触发数据库与服务器的同步。我实际上使用 Volley 进行网络调用并同步数据,但从我读到的
我正在使用 AbstractThreadedSyncAdapter 将我的 Android 手机中的一些数据与服务器同步。当用户使用手机的应用程序登录时,我会执行第一次同步,以便用户在开始使用我的
我有一个 SyncAdapter 类,它连接到 MQTT 代理并为服务器发布负载以接收负载。但是,似乎即使调用了 onPerformSync() 方法,也无法访问互联网。我认为使用 SyncAdapt
除了一件事,我的 syncadapter 运行良好。用户安装应用程序后,我的应用程序会同步两次。稍后,如果我在“设置”中手动同步它,它只会按预期同步一次。这只是发生这种情况的应用程序的第一次运行。 这
我关注这个标题Why does ContentResolver.requestSync not trigger a sync?但同步方法不会出现在设置屏幕的帐户和同步中。我认为 onPerformSy
正在尝试为我的内容提供者设置一个同步适配器/服务。内容提供者工作正常,能够毫无问题地存储数据。 我有一个向我的内容解析器注册的内容观察器,并且在数据更改后调用 onChange 方法。 但是,在我调用
我正在尝试实现同步适配器,我已经关注了This guide. 但是 onPerformSync() 在 ContentResolver.requestSync() 调用之后没有被触发。 我也尝试了一些
我正在创建一个 Android 应用程序,它向用户提供一个按钮来刷新数据库,以便本地数据和服务器上的数据保持同步。 我已经创建了帐户验证器、内容提供器并通过服务绑定(bind)了它们。 Content
我已经使用 Android 设置了一个 SyncAdapter sample project 、Steele 的两部分教程(1 和 2)和 Google I/O 2010 关于 REST 应用程序的演
当我将联系人添加到我的电话簿时,同步适配器的 onPerformSync() 方法被触发。但我无法弄清楚如何在该方法中检索新添加的联系方式。请帮助我提供一些指导和代码 fragment 。感谢您的帮助
我正在通过 SyncAdapter onPerformSync 发送几个改造调用,并且我试图通过通过 try/catch sleep 语句发送出去来调节 http 调用。但是,这会阻塞 UI,并且只有
你们能不能帮我一下。我一直在尝试在 onPerformSyn 中运行 yelp api。我怎么可能这样做。?我收到以下错误。 “没有空的构造函数”我正在使用这个 post尝试创建构造函数,但它对我不起
我使用 SyncAdapter 将我的数据与服务器同步。一切正常,但我很难向 SyncAdapter 发送额外内容。 在我的 ContentObserver 中,我使用以下代码向 SyncAdapte
我收到了大量来自用户的错误报告,这些错误报告来自强制关闭对话框,内容如下: java.lang.ExceptionInInitializerError at com.MyRequest.execute
我正在尝试使用 AbstractThreadedSyncAdapter 将我的本地数据库与我的服务器同步。我已经按照找到的指南进行操作,但无法正常工作。适用于 Android 2.2。 我的身份验证器
我是一名优秀的程序员,十分优秀!