我如何使用Android SyncAdapter?如何使用、Android、SyncAdapter

2023-09-13 02:23:54 作者:美少女壮士

我试着去了解Android的同步逻辑。我不明白的是该文件 syncadapter.xml 包含在Android SDK中的示例项目 SampleSyncAdapter 。如果下载了SDK的样品应当在以下文件夹:

SDK/android-sdk-PLATFORM/samples/android-VERSION/SampleSyncAdapter/res/xml/syncadapter.xml

我看了,内容提供者的权威应该是一个字符串或引用的资源。到底是什么内容权威,哪里是 com.android.contacts ?下面是该文件的内容(W / O型许可信息和意见,空气污染指数16)。

 <同步适配器的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:contentAuthority =com.android.contacts
    机器人:accountType =com.example.android.samplesync
    机器人:supportsUploading =假
    机器人:userVisible =真
/>
 

解决方案 SyncMate Expert Mac版 SyncMate Expert for mac下载 V8.0.469 PC6苹果网

有,你可以做一个SyncAdapter时使用的两种基本方法:

填充数据到现有的ContentProvider. 创建你自己的ContentProvider来存储新的数据类型。

,前者是在这个例子中的应用程序正在进行。他们有一些网站,有联系人的列表,并且它们要存储那些连同设备上的其他联系人。在两种情况下,这个方法的所有作品是通过三个部件之间的关系:

ContentProvider的,用于存储数据。 系统SyncAdapter,这与远程服务器获取数据,以投入的ContentProvider进行通信。 而Android ContentResolver的,这数字如何配对SyncAdapters和ContentProviders。

这是Android设备可以有许多不同ContentProviders和许​​多不同的SyncAdapters。因为一个ContentResolver的可能不相同的部分的的apk 的作为SyncAdapter,ContentResolver的是,找到正确的ContentProvider存储的给定类型的数据的系统服务。为此,它使用的ContentAuthority字符串,它唯一标识一个特定的ContentProvider。此外,每个的ContentProvider必须在的Andr​​oidManifest.xml 声明它确保它能够由ContentResolver的发现。在这个声明中,您可以指定的ContentProvider是否可以使用其他应用程序,请参见:机器人:出口

 <供应商
    机器人:名称=。CustomProvider
    机器人:当局=com.example.app.provider
    机器人:出口=假
    机器人:多进程=真正的>
< /供应商>
 

在这种情况下,使用现有的ContentProvider,则需要看平台文档,看看他们使用什么ContentAuthority字符串,并使用相同的字符串。如果你创建自己的ContentProvider,你只需要确保你创建ContentAuthority是独一无二的。要做到这一点,最好的方法是使用你的域名(Java类样式)管理局部分。它们写在相反的顺序。这一点在他们的榜样...... com.android.contacts

I try to understand the Android synchronization logic. What I don't understand is the file syncadapter.xml contained in the Android SDK sample project SampleSyncAdapter. If you downloaded the SDK samples it should be in the following folder:

SDK/android-sdk-PLATFORM/samples/android-VERSION/SampleSyncAdapter/res/xml/syncadapter.xml

I read, the authority of a content provider should be a string or a reference to a resource. What exactly is the content authority and where is com.android.contacts? Here is the content of the file (w/o license information and comments, API level 16).

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="com.android.contacts"
    android:accountType="com.example.android.samplesync"
    android:supportsUploading="false"
    android:userVisible="true"
/>

解决方案

There are two basic methods you can use when making a SyncAdapter:

Fill data into an existing ContentProvider. Create your own ContentProvider to store a new kind of data.

The former is what's going on in this example app. They have some website that has a list of contacts, and they want to store those along with the other contacts on the device. In either case, the way this all works is through a relationship between three components:

A ContentProvider, which stores the data. A SyncAdapter, which communicates with a remote server to obtain data to put into the ContentProvider. The Android ContentResolver, which figures out how to pair up SyncAdapters and ContentProviders.

An Android device can have many different ContentProviders and many different SyncAdapters. Since a ContentResolver may not be part of the same .apk as a SyncAdapter, ContentResolver is a system service that finds the right ContentProvider to store a given kind of data. It does this using the ContentAuthority string, which uniquely identifies one specific ContentProvider. Moreover, each ContentProvider must be declared in AndroidManifest.xml which ensures that it can be found by the ContentResolver. Within this declaration you can specify whether the ContentProvider can be used by other applications, see: android:exported.

<provider
    android:name=".CustomProvider"
    android:authorities="com.example.app.provider"
    android:exported="false"
    android:multiprocess="true" >
</provider>

In this case, using an existing ContentProvider, you will need to look at the platform documentation to see what ContentAuthority string they use, and use the same string. If you're creating your own ContentProvider, you just need to ensure that the ContentAuthority you create is unique. The best way to do this is to use parts of your domain name (java class style) in the Authority. Write them in the reverse order. This is illustrated in their example... com.android.contacts.