What are Android SyncAdapter contentAuthority and accountType?

I create a custom Android SyncAdapter and fall into the trap, following the example of the SampleSyncAdapter SDK. - I create my equivalent xml/syncadapter.xml . Here are the details that I'm confused about:

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

The AbstractThreadedSyncAdapter documentation says:

Attributes android:contentAuthority and android:accountType indicate which content authority and account for which this synchronization adapter serves.

The documentation is circular in the sense that it does not say anything, this name does not yet tell you. I get the impression that both will begin with the name of my company com.acme. but from there I do not know. I suspect that strings can be anything if they are globally unique so as not to conflict with any other applications that may reside on the same device. I guess this means that I will need to use these exact lines elsewhere in my code. However, I would like to know where I will need these lines ?! I tried grep for com.android.contacts and the previously mentioned file is the only place it uses, I can find. Therefore, it is not possible to determine how contentAuthority used by looking at an example.
If so, can I put them in a string resource and specify them by resource identifier, where necessary? What exactly are these attributes and how are they used? Is there a better way to find out what values ​​I should choose for my application for these and other fields?

+6
source share
2 answers

To understand what you need, you need to look at the ContentProvider documentation :

It states: "it identifies the content provider. For third-party applications, this must be the fully qualified name of the class (lowercase) to ensure uniqueness. Power is declared in the element’s authority attribute"

The type of account is the identifier of your Authenticator, which will be used, for example, by AccountManager clients to call getAccountsByType(String) .

For SampleSyncAdapter:

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

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

Thus, Content-Authority indicates which content provider will be synchronized locally, and the accountType type indicates which authenticator will be used for remote access to data. The accountType type is also used to get the specific contents of the sync-uri adapter.

For example, if you want to start synchronization, you need to call requestSync as follows:

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

At the same time, to create 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(); 

Take a look at android-sync-adapter


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

Authorization Design

A provider usually has a single authority that serves as its Android internal name. To avoid conflicts with other providers, you must use your domain’s Internet ownership right (in reverse order) as the basis for your provider. Since this recommendation is also true for Android Package Names, you can define the authority of your provider as an extension of the name of the package that contains the provider. For example, if your Android package name is com.example.<appname> , you must give your provider the authority com.example.<appname>.provider .

+5
source

The android: contentAuthority attribute in the SyncAdapter syncadapter.xml metadata file must match the android: authority attribute for your provider declaration in your manifest. Make this value your application package name with the string ".provider" added to it. This is from the Android 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 file

 <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"/> 
0
source

Source: https://habr.com/ru/post/903870/


All Articles