我正在编写一个应用程序,它使用同步适配器来同步数据。
我已经阅读了这些文档,我非常肯定我理解它是如何工作的。我的大部分同步适配器都是按照Udi编写的这个伟大的向导来工作的。
然而,我确实有一个问题,我似乎无法解决,这是自动同步时,我的应用程序被安装。
当我的应用程序运行起来时,它会创建一个同步适配器和一个帐户,完成所有您希望它做的工作,这是很棒的。然而,如果我转到设置>帐户>‘我的应用’,同步是关闭的。不管怎样,我可以让这个自动启用吗?
Screenshot of Accounts > 'My App'
设置同步适配器时,我的代码如下所示:
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);,但这似乎没有任何效果。
我有android.permission.WRITE_SYNC_SETTINGS在我的AndroidManifest中声明的权限,所以这不是问题。我的项目中也有同步服务和xml文件。我已经附上了下面这些代码。
还有其他人遇到过类似的问题吗?会不会是权限问题?如果有人有任何建议的话,我很乐意尝试。谢谢。
AndroidManifest.xml
<service
android:name=".syncadapter.CalendarSyncService"
android:exported="true"
android:process=":sync">
<intent-filter>
<action android:name="android.content.SyncAdapter"/>
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="@xml/calendar_syncadapter"/>
</service>syncadapter.xml
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="com.companyname.rostering"
android:allowParallelSyncs="true"
android:contentAuthority="com.android.calendar"
android:isAlwaysSyncable="true"
android:supportsUploading="true"
android:userVisible="true"/>发布于 2016-05-12 10:38:59
我终于发现是什么引起了我的问题。
在我的syncadapter.xml中,我的内容权限设置为android:contentAuthority="com.android.calendar"。
但是,在我的代码中,我将同步设置为自动使用ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true); --我的内容授权值实际上被设置为与我在XML中的内容权限不同的内容。
设置同步适配器时的内容权限必须与XML中使用的内容权限匹配。
发布于 2016-04-20 09:13:54
将清单更改为如下所示:
<service
android:name=".SyncService"
android:exported="true"
>
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="@xml/syncadapter" />
</service>在您的syncadapter.xml中有以下属性:
<?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:contentAuthority="@string/content_authority"
android:accountType="@string/sync_account_type"
android:isAlwaysSyncable="true" />我查看了提供的教程链接,android:isAlwaysSyncable是false,所以我认为实现它可以解决问题。
更新
另一件事是,如果您在比KITKAT更好的版本上安装该应用程序,那么您可能想改变调用addPeriodicSync的方式:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// we can enable inexact timers in our periodic sync
SyncRequest request = new SyncRequest.Builder().
syncPeriodic(syncInterval, flexTime).
setSyncAdapter(account, authority).
setExtras(new Bundle()).build();
ContentResolver.requestSync(request);
} else {
ContentResolver.addPeriodicSync(account,
authority, new Bundle(), syncInterval);https://stackoverflow.com/questions/36738114
复制相似问题