首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安装同步适配器时启用自动同步

安装同步适配器时启用自动同步
EN

Stack Overflow用户
提问于 2016-04-20 08:32:52
回答 2查看 4.8K关注 0票数 7

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

我已经阅读了这些文档,我非常肯定我理解它是如何工作的。我的大部分同步适配器都是按照Udi编写的这个伟大的向导来工作的。

然而,我确实有一个问题,我似乎无法解决,这是自动同步时,我的应用程序被安装。

当我的应用程序运行起来时,它会创建一个同步适配器和一个帐户,完成所有您希望它做的工作,这是很棒的。然而,如果我转到设置>帐户>‘我的应用’,同步是关闭的。不管怎样,我可以让这个自动启用吗?

Screenshot of Accounts > 'My App'

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

代码语言:javascript
复制
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

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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"/>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-12 10:38:59

我终于发现是什么引起了我的问题。

在我的syncadapter.xml中,我的内容权限设置为android:contentAuthority="com.android.calendar"

但是,在我的代码中,我将同步设置为自动使用ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true); --我的内容授权值实际上被设置为与我在XML中的内容权限不同的内容。

设置同步适配器时的内容权限必须与XML中使用的内容权限匹配。

票数 5
EN

Stack Overflow用户

发布于 2016-04-20 09:13:54

将清单更改为如下所示:

代码语言:javascript
复制
     <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中有以下属性:

代码语言:javascript
复制
<?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:isAlwaysSyncablefalse,所以我认为实现它可以解决问题。

更新

另一件事是,如果您在比KITKAT更好的版本上安装该应用程序,那么您可能想改变调用addPeriodicSync的方式:

代码语言:javascript
复制
    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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36738114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档