什么是Android的SyncAdapter contentAuthority和accountType? [英] What are Android SyncAdapter contentAuthority and accountType?

查看:299
本文介绍了什么是Android的SyncAdapter contentAuthority和accountType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个定制的Andr​​oid SyncAdapter并创下继SDK的例子SampleSyncAdapter一个障碍。 - 我创造我等价的 XML / syncadapter.xml 。下面是我感到困惑的部分约:

I'm creating a custom Android SyncAdapter and hit a snag following the SDK example "SampleSyncAdapter". - I'm creating my equivalent of the xml/syncadapter.xml. Here's the parts I'm confused about:

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

的AbstractThreadedSyncAdapter 国文档:

The documentation of AbstractThreadedSyncAdapter states:

安卓contentAuthority 安卓accountType 属性   说明哪些内容权威,哪些帐户类型此同步   适配器供应。

The android:contentAuthority and android:accountType attributes indicate which content authority and for which account types this sync adapter serves.

该文档是圆形的,因为它没有这个名字已经不告诉你说。我得到的IM pression既将开始与我公司的名称 com.acme。但是从那里,我不知道。我怀疑字符串可以是任何东西,只要他们是全球唯一的,以免有可能在同一个设备上的任何其他应用程序冲突。我想这意味着我需要在我的code在其他地方使用这些精确匹配。不过,我想知道,在这里我需要这些字符串?我想对grep com.android.contacts 和pviously提到的文件$ P $是,我能找到它使用的唯一的地方。因此,它是不可能告诉如何使用 contentAuthority 用于通过查看例子。
如果是的话,我可以把他们两个在一个字符串资源,并在需要的地方引用它们通过资源ID?究竟什么是这些属性以及如何使用它们?有没有更好的方法来找出什么样的价值观,我应该选择适合自己的应用程序,这些和其他领域?

The documentation is circular in that it says nothing the name doesn't already tell you. I get the impression that both will start with my company's name com.acme. but from there I have no clue. I suspect the strings can be anything, as long as they are globally unique so as not to conflict with any other apps that could be on the same device. I assume this means I will need to use these exact strings elsewhere in my code. However, I'd like to know, where I will need these strings?! I tried to grep for com.android.contacts and the previously mentioned file is the only place it is used, I can find. So it is impossible to tell how the contentAuthority is used by looking at the example.
If so, can I put them both in a string resource and reference them in by resource ID where needed? Exactly what are these attributes and how are they used? Is there any better way to figure out what values I should choose for my own application for these and other fields?

推荐答案

要知道你需要什么样的权力是看的 ContentProvider的的文档:

To understand what the authority is you need to see the documentation of ContentProvider:

它指出:。它标识内容提供商对于第三方应用程序,这应该是一个完全合格的类名(减少为小写),以确保其唯一性的权力在要素当局宣称属性

It states: "it identifies the content provider. For third-party applications, this should be a fully-qualified class name (reduced to lowercase) to ensure uniqueness. The authority is declared in the element's authorities attribute"

帐户类型是你的身份验证的识别器,将通过客户端可以使用例如的AccountManager 的调用 getAccountsByType(字符串)

The account type is an identificator of your Authenticator that will be used for example by the clients of the AccountManager to call getAccountsByType(String).

对于 SampleSyncAdapter 的:

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

机器人:accountType相同,一个由认证定义

android:accountType is the same as that one defined by the authenticator.

所以该内容 - 管理局指定哪些内容提供商将在本地同步,accountType指定哪个认证者将被用于远程访问数据。 所述accountType还用于获得同步适配器的具体内容的URI。

So the content-Authority specifies which content provider will be synchronized locally and the accountType specifies which authenticator will be used to access the data remotely. The accountType is also used to obtain the sync adapter's specific content-uri.

例如,当你要开始同步,你需要调用的 requestSync 的是这样的:

For example when you want to start a sync you need to call requestSync like this:

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

同时,以构建内容-URI为您的同步适配器,你可以使用这样的:

At the same time to build the content-uri for your sync adapter you can use something like:

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

有一个外观 Android的同步适配器

与此同时,pviously提到ContentProvider的文档中的$ P $进行了修订。该最新版本状态:

Meanwhile, the previously mentioned ContentProvider documentation has been revised. The latest version states:

设计一个权威

一个提供者,通常有一个单一的机构,作为其   Android的内部名称。为了避免与其他供应商的冲突,你   应该使用互联网域名的所有权(反向)为基础的   供应商权限。由于这一建议也适用于   Android包名,您可以定义您的供应商权限为   扩展包含提供程序包的名称。对于   例如,如果你的Andr​​oid包名为 com.example<应用程序的名字> ,你   应该给你的供应商权限    com.example<应用程序的名字>。.provider

A provider usually has a single authority, which serves as its Android-internal name. To avoid conflicts with other providers, you should use Internet domain ownership (in reverse) as the basis of your provider authority. Because this recommendation is also true for Android package names, you can define your provider authority as an extension of the name of the package containing the provider. For example, if your Android package name is com.example.<appname>, you should give your provider the authority com.example.<appname>.provider.

这篇关于什么是Android的SyncAdapter contentAuthority和accountType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆