android - AbstractThreadedSyncAdapter->onPerformSync 调用时间过长

标签 android synchronization

我正在使用 AbstractThreadedSyncAdapter 将我的 Android 手机中的一些数据与服​​务器同步。当用户使用手机的应用程序登录时,我会执行第一次同步,以便用户在开始使用我的应用程序之前在手机中准备好所有数据。我用 eclipse 的模拟器测试了它,它工作正常,但是当我在我的手机上测试它时,调用 onPerformSync 函数的时间太长(比如一分钟或更长时间)。

这是我在身份验证器 Activity 中使用的代码:

        Bundle params = new Bundle();
        params.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
        params.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false);
        params.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
        ContentResolver.addPeriodicSync(account, "com.pfc.authority", params, 30);
        ContentResolver.setSyncAutomatically(account, "com.pfc.authority", true);

        ContentResolver.requestSync(account,"com.pfc.authority",params);

我的 AbstractThreadedSyncAdapter 的构造函数被调用得非常快,但是我的应用程序仍在等待同步完成,就像我说的 onPerformSync 函数启动时间太长,我不知道为什么我的手机太慢了很长的时间来调用它。

这是日志(针对我的应用程序过滤)。我将 Log.d 放在构造函数和 onPerformSyn 函数中:

11:47:49 SweetSyncAdapter: constructor
11:48:01 SweetSyncAdapter: constructor
11:48:36 SweetSyncAdapter: onPerformSync

我查看了完整的日志(没有过滤)并且在执行我的应用程序时没有错误或警告,但我看到有另一个同步正在进行(音乐、日历等)。这可能是问题所在吗?

我用两个不同的 ROM(2.3.6 JVU 和现在的 ICS)测试了它,结果是一样的。有人知道为什么会这样吗?

PD:现在我看到注销时发生了同样的情况(我在用户注销时执行同步),并且手机已经等待调用 onPerformSync 了 5 分钟。

最佳答案

创建帐户后,我请求与以下行同步。

Bundle bundle = new Bundle();
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_FORCE, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
ContentResolver.requestSync(account, authority, bundle);

我认为SYNC_EXTRAS_FORCE 会对您有所帮助

关于android - AbstractThreadedSyncAdapter->onPerformSync 调用时间过长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11502043/

相关文章:

Android SQLite 插入错误

android - 服务结束时发出通知声

synchronization - Unison 移动 root 与 rootalias

mysql - Wordpress 后期同步/发布到生产环境

java - 屏幕关闭/打开后,同步(和 volatile )无法按预期工作

android,计算两地之间的距离,谷歌距离矩阵

javascript - 清理一大段 Javascript 代码

android - 在 ViewAnimator 中的按钮上执行(单击())后,Espresso 测试卡住/不活动

c# - .NET 线程池 QueueUserWorkItem 同步

django - 使用 django 同步数据库内容