Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the Android SyncAdapter?

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" /> 
like image 244
xpepermint Avatar asked Oct 22 '10 07:10

xpepermint


People also ask

Where is main method in Android?

The main() method is in the Android framework class android. app. ActivityThread . This method creates the Main (UI) Thread , sets up a Looper on it and starts the event loop.

What is offline synchronization in android?

Offline sync allows end users to interact with a mobile app—viewing, adding, or modifying data—even when there's no network connection. Changes are stored in a local database. Once the device is back online, these changes are synced with the remote backend.

What is sync service app?

The Sync apps are available for Android, iPhone and iPad, and make it easy for you to access your files right from your mobile device. The Sync apps are free, and provide the following features: Access your files in Sync from anywhere. Use third-party apps on your device to open and edit files in Sync.

What is a SYNC adapter in Android?

The sync adapter component in your app encapsulates the code for the tasks that transfer data between the device and a server. Based on the scheduling and triggers you provide in your app, the sync adapter framework runs the code in the sync adapter component.

What is the use of sync adapter permissions in Android?

Allows your app to read the current sync adapter settings. For example, you need this permission in order to call getIsSyncable () . Allows your app to control sync adapter settings. You need this permission in order to set periodic sync adapter runs using addPeriodicSync ().

How do I use the SYNC adapter Constructors?

Use the constructors to run setup tasks each time your sync adapter component is created from scratch, just as you use Activity.onCreate () to set up an activity. For example, if your app uses a content provider to store data, use the constructors to get a ContentResolver instance.

How do I run a code in the SYNC adapter?

* Specify the code you want to run in the sync adapter. The entire * up your own background processing. * Put the data transfer code here. * Specify the code you want to run in the sync adapter. The entire * up your own background processing. * Put the data transfer code here.


1 Answers

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

  1. Fill data into an existing ContentProvider.
  2. 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:

  1. A ContentProvider, which stores the data.
  2. A SyncAdapter, which communicates with a remote server to obtain data to put into the ContentProvider.
  3. 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.

like image 136
jcwenger Avatar answered Nov 04 '22 04:11

jcwenger