【问题标题】:Android - Enable automatic sync when sync adapter is setupAndroid - 设置同步适配器时启用自动同步
【发布时间】:2016-04-20 08:32:52
【问题描述】:

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

我已经阅读了文档,并且我很确定我了解它是如何工作的。通过遵循 Udi Cohen 撰写的 great guide,我的大部分同步适配器都能正常工作。

但是,我确实有一个问题似乎无法解决,那就是在安装我的应用时自动启用同步。

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

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);,但这似乎没有任何效果。

我在我的 AndroidManifest 中声明了android.permission.WRITE_SYNC_SETTINGS 权限,所以这不是问题。我的项目中也有同步服务和 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"/>

【问题讨论】:

    标签: java android android-syncadapter


    【解决方案1】:

    我最终发现了导致我的问题的原因。

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

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

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

    【讨论】:

    • 这里也一样。在我的syncadapter.xml 中我有android:contentAuthority="com.android.calendar",而在我的活动中我有ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);。两者都将其更改为 com.android.calendar 修复了它。
    【解决方案2】:

    将清单更改为如下所示:

         <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:isAlwaysSyncablefalse,所以我认为让它成为现实将解决问题

    更新

    另一件事是,如果您在高于 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);
    

    【讨论】:

    • 我的清单和 xml 适配器中确实设置了上述所有内容。我会更新我的问题。
    • 感谢您的建议。该应用程序仅支持 KITKAT 及更高版本,所以这绝对是我应该添加到我的代码中的东西,我现在已经完成了。但是它仍然不能解决我原来的问题。
    • @JamesNWarner 您是否尝试过卸载并重新安装更改后的应用程序?
    • 我确实有。不会尝试任何其他方式。
    • @JamesNWarner 我提供的代码实际上来自我的一个项目,该项目在我的设备上运行良好,但不知道还有什么建议!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多