java同步设定_java – Android – 在设置同步适配器时启用自动同步

我正在编写一个使用同步适配器来同步数据的应用程序.

我已经阅读了文档,我很确定我理解它是如何工作的.我的大部分同步适配器都是按照Udi Cohen编写的great guide来完成的.

但是,我确实有一个问题似乎无法解决,而且在安装我的应用程序时会自动启用同步.

当我的应用程序运行时,它会创建一个同步适配器和一个帐户,完成您希望它完成的所有工作,这很棒.但是,如果我转到设置>帐户> ‘我的应用’,同步已关闭.无论如何我可以让它自动启用吗?

设置同步适配器时,我的代码如下所示:

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

ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true);

// Recommend a schedule for auto sync

ContentResolver.addPeriodicSync(account, CONTENT_AUTHORITY, new Bundle(), SYNC_FREQUENCY);

newAccount = true;

}

通过阅读同步适配器,我相信ContentResolver.setSyncAutomatically()是用于自动启用同步的正确方法.

我也尝试过设置ContentResolver.setMasterSyncAutomatically(true);但这似乎没有任何影响.

我在AndroidManifest中声明了android.permission.WRITE_SYNC_SETTINGS权限,所以这不是问题所在.我的项目中也有同步服务和xml文件.我在下面附上了这些代码.

还有其他人有过类似的问题吗?这可能是权限问题吗?

如果有人有任何建议我会很乐意尝试.谢谢.

AndroidManifest.xml中

android:name=".syncadapter.CalendarSyncService"

android:exported="true"

android:process=":sync">

android:name="android.content.SyncAdapter"

android:resource="@xml/calendar_syncadapter"/>

syncadapter.xml

android:accountType="com.companyname.rostering"

android:allowParallelSyncs="true"

android:contentAuthority="com.android.calendar"

android:isAlwaysSyncable="true"

android:supportsUploading="true"

android:userVisible="true"/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值