6

我正在创建一个自定义 Android SyncAdapter,并在 SDK 示例“SampleSyncAdapter”之后遇到了问题。- 我正在创建我的xml/syncadapter.xml. 这是我感到困惑的部分:

android:contentAuthority="com.android.contacts"
android:accountType="com.example.android.samplesync"

AbstractThreadedSyncAdapter的文档指出:

和属性指示此同步适配器服务于哪个内容授权和帐户类型android:contentAuthorityandroid:accountType

该文档是循环的,因为它没有说明名称尚未告诉您的内容。我的印象是两者都将以我公司的名称开头,com.acme.但从那里我一无所知。我怀疑字符串可以是任何东西,只要它们是全局唯一的,以免与同一设备上的任何其他应用程序冲突。我认为这意味着我需要在代码的其他地方使用这些确切的字符串。但是,我想知道,我将在哪里需要这些字符串?!我尝试 grep forcom.android.contacts并且前面提到的文件是唯一使用它的地方,我可以找到。因此,无法contentAuthority通过查看示例来判断它是如何使用的。
如果是这样,我可以将它们都放在字符串资源中并在需要的地方通过资源 ID 引用它们吗?这些属性究竟是什么以及它们是如何使用的?有没有更好的方法来确定我应该为自己的这些和其他领域的应用程序选择什么值?

4

2 回答 2

5

要了解权限是什么,您需要查看ContentProvider 的文档

它声明:“它标识内容提供者。对于第三方应用程序,这应该是一个完全限定的类名(简化为小写)以确保唯一性。权限在元素的权限属性中声明”

帐户类型是您的 Authenticator 的标识符,例如AccountManager的客户端将使用它来调用getAccountsByType(String).

对于SampleSyncAdapter

android:contentAuthority="com.android.contacts"
android:accountType="com.example.android.samplesync"

android:accountType 与authenticator 定义的相同。

所以 content-Authority 指定了哪个内容提供者将在本地同步,而 accountType 指定了哪个身份验证器将用于远程访问数据。accountType 还用于获取 Sync Adapter 的特定 content-uri。

例如,当您想要开始同步时,您需要像这样调用requestSync

final Account account = new Account(accountName, ACCOUNT_TYPE);
ContentResolver.requestSync(account, CONTENT_AUTHORITY, new Bundle());

同时为你的同步适配器构建 content-uri,你可以使用类似的东西:

Uri CONTENT_URI = ContactsContract.RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(RawContacts.ACCOUNT_NAME, accountName).appendQueryParameter(RawContacts.ACCOUNT_TYPE, SyncAdapter.ACCOUNT_TYPE).build();

看看android-sync-adapter


同时,对前面提到的 ContentProvider 文档进行了修订。最新版本指出:

设计权威

提供者通常只有一个权限,作为其 Android 内部名称。为避免与其他提供商发生冲突,您应该使用 Internet 域所有权(反向)作为您的提供商权限的基础。由于此建议也适用于 Android 包名称,因此您可以将提供者权限定义为包含提供者的包名称的扩展。例如,如果你的 Android 包名是com.example.<appname>,你应该给你的提供者权限 com.example.<appname>.provider

于 2011-12-15T20:43:20.513 回答
0

The android:contentAuthority attribute in your SyncAdapter meta data file syncadapter.xml should match the android:authorities attribute for your provider declaration in your Manifest. Make this value your app's package name with the string ".provider" appended to it. This is from Android's developer site http://developer.android.com/training/sync-adapters

So in your Manifest:

<provider
    android:name="com.example.android.datasync.provider.StubProvider"
    android:authorities="com.example.android.datasync.provider"
    android:exported="false"
    android:syncable="true"/>

And in your syncadapter.xml

<sync-adapter
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="com.example.android.datasync.provider"
    android:accountType="com.android.example.datasync"
    android:userVisible="false"
    android:supportsUploading="false"
    android:allowParallelSyncs="false"
    android:isAlwaysSyncable="true"/>
于 2014-12-08T15:53:14.557 回答