我正在创建一个自定义的Android SyncAdapter,并在SDK示例“SampleSyncAdapter”之后遇到麻烦。 - 我正在创建我的等效xml/syncadapter.xml
。这是我很困惑的部分:
android:contentAuthority="com.android.contacts"
android:accountType="com.example.android.samplesync"
documentation of AbstractThreadedSyncAdapter州:
android:contentAuthority
和android:accountType
属性 指示哪个内容权限以及此同步的帐户类型 适配器服务。
文档是循环的,因为它没有说明这个名称没有告诉你。我的印象是两者都会以我公司的名字com.acme.
开头,但从那里我就不知道了。我怀疑字符串可以是任何东西,只要它们是全局唯一的,以免与可能在同一设备上的任何其他应用程序冲突。我认为这意味着我需要在我的代码中的其他地方使用这些确切的字符串。但是,我想知道,我需要这些字符串在哪里?!我试图grep com.android.contacts
并且前面提到的文件是它唯一使用的地方,我可以找到。因此,通过查看示例,无法确定如何使用contentAuthority
如果是这样,我可以将它们都放在字符串资源中并在需要时通过资源ID引用它们吗?这些属性究竟是什么以及它们是如何使用的?有没有更好的方法来确定我应该为这些和其他领域的应用程序选择哪些值?
答案 0 :(得分:5)
要了解权限,您需要查看documentation of ContentProvider:
它声明:“它标识内容提供者。对于第三方应用程序,这应该是一个完全限定的类名(缩小为小写)以确保唯一性。权限在元素的权限属性中声明”
帐户类型是您的身份验证器的标识符,例如 AccountManager 的客户端将用于调用getAccountsByType(String)
。
对于 SampleSyncAdapter :
android:contentAuthority="com.android.contacts"
android:accountType="com.example.android.samplesync"
android:accountType与验证者定义的那个相同。
因此,content-Authority指定将在本地同步哪个内容提供者,而accountType指定将使用哪个身份验证器远程访问数据。 accountType还用于获取同步适配器的特定内容-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();
与此同时,前面提到的ContentProvider文档已经过修订。 latest version州:
设计权限
提供商通常只有一个权限,作为其权限 Android内部名称。为了避免与其他提供商发生冲突 应该使用互联网域名所有权(反向)作为您的基础 提供者权限。因为这个建议也是如此 Android软件包名称,您可以将您的提供商权限定义为 包含提供程序的程序包名称的扩展名。对于 例如,如果您的Android软件包名称为
com.example.<appname>
,则为您 应该给你的提供者权限com.example.<appname>.provider
。
答案 1 :(得分:0)
SyncAdapter元数据文件syncadapter.xml中的android:contentAuthority属性应与Manifest中提供程序声明的android:authorities属性匹配。使用字符串&#34; .provider&#34;将此值作为应用程序的包名称。附加到它。这是来自Android的开发者网站http://developer.android.com/training/sync-adapters
所以在你的Manifest:
<provider
android:name="com.example.android.datasync.provider.StubProvider"
android:authorities="com.example.android.datasync.provider"
android:exported="false"
android:syncable="true"/>
在你的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"/>