android – 使用addPeriodicSync时,停止同步适配器最初同步

我在我的项目中使用同步适配器,它将定期同步.要为同步适配器创建帐户,我使用以下代码.

我面临的问题是此代码触发了初始同步.文档中没有提到此代码将使同步最初运行.

事实上即使在谷歌示例项目中也有额外的代码用于触发我已删除的初始同步.

我使用了此示例中的代码:
http://developer.android.com/samples/BasicSyncAdapter/index.html

即使我添加命令ContentResolver.cancelSync(account,null);同步适配器仍然运行.

如何阻止同步适配器最初同步.它应该在同步间隔周期过去后第一次同步.

Account account = new Account(context.getPackageName(),context.getPackageName());

AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

if (accountManager.addAccountExplicitly(account,null,null)) {

        // Inform the system that this account supports sync
        ContentResolver.setIsSyncable(account,context.getPackageName(),1);

        // Inform the system that this account is eligible for auto sync when the network is up
        ContentResolver.setSyncAutomatically(account,true);

        // Recommend a schedule for automatic synchronization. 
        // The system may modify this based
        // on other scheduled syncs and network utilization.
        ContentResolver.addPeriodicSync(account,Bundle.EMPTY,AppConstants.SYNC_INTERVAL);
}

解决方法

您可以在第一次手动同步后安排未来事件.
private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

private void setDelayedAutoSync() {
            ScheduledFuture<?> countdown = scheduler.schedule(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG,"Out of time!");
                ContentResolver.setSyncAutomatically(account,content_authority,true);
                ContentResolver.addPeriodicSync(account,new Bundle(),SYNC_FREQUENCY_CONSTANT);
        },SYNC_FREQUENCY_CONSTANT,TimeUnit.SECONDS);
    }

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...