I first introduced the account and synchronization of account management, this article introduces another part, is the use of Android to provide sync synchronization mechanism.
In fact, the use of sync mechanism is similar to the account management introduced in the previous blog post, and is also based on the binder mechanism of cross-process communication. First it needs a service, which provides an action to the system so that the system can find it, and then inherits and implements Abstractthreadedsyncadapter, This class contains the implementation of the Isyncadapter.stub inner class, which encapsulates the remote interface call, the class Getsyncadapterbinder () method, which returns the IBinder form of the inner class. To make a remote call to the ABSTRACTTHREADEDSYNCADAPTE, a service registration is required in manifest, and Meta-data is specified, and the meta-data is an XML file. In the Samplesyncadapter instance, its name is Syncadapter.xml, which specifies the account number and the contentprovider being monitored. These files are described in the following separate sections:
Syncservice.java
SyncService is a service that inherits the common service and is used to serve the remote process, returning IBinder in the Onbind method.
public class SyncService extends Service {
Private static Final Object Ssyncadapterlock = new Object ();
private static SyncAdapter ssyncadapter = null;
@Override
public void OnCreate () {
Synchronized (Ssyncadapterlock) {
if (Ssyncadapter = = null) {
Ssyncadapter = new SyncAdapter (Getapplicationcontext (), true);
}
}
}
@Override
Public IBinder Onbind (Intent Intent) {
return Ssyncadapter.getsyncadapterbinder ();
}
}
Its action value is Android.content.SyncAdapter, which is registered as follows:
<service
Android:name= ". SyncAdapter. SyncService "
Android:exported= "true" >
<intent-filter>
<action
Android:name= "Android.content.SyncAdapter"/>
</intent-filter>
<meta-data
Android:name= "Android.content.SyncAdapter"
Android:resource= "@xml/syncadapter"/>
</service>
An adapter can only synchronize one authority, and if you want to synchronize multiple authority for an account, you may register multiple Sync-adapter with the same account with the system.
Syncadapter.xml
The Syncadapter.xml file specifies the authority of the ContentProvider that this service listens on, and also specifies the type of account that listens for this authority, which is related to the account type in the previous article.
<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"
/>
android:contentauthority Specifies that the ContentProvider to be synchronized has a android:authorities attribute in its androidmanifest.xml file.
android:accounttype Indicates the type of account that is being synchronized.
attributes indicate which content authority and for which account types this sync adapter serves.
android:uservisible setting is displayed in Settings
Defaults to True and controls whether, or not, this sync adapter shows on the Sync Settings screen.
android:supportsuploading Set Whether you must notifychange notifications to synchronize
Defaults to True and if true a upload-only sync would be requested for all syncadapters associated with an authority Whene Ver that authority ' s content provider does a notifychange (Android.net.Uri, Android.database.ContentObserver, Boolean) With Synctonetwork set to true.
Android:allowparallelsyncs Support simultaneous simultaneous multiple accounts
Defaults to False and if True indicates this sync adapter can handle syncs for multiple accounts at the same time. Otherwise the SyncManager would wait until the sync adapter is not on use before requesting the IT sync a account ' s data.
android:isalwayssyncable Set the issyncable of all accounts to 1
Defaults to False and if True tells the SyncManager to intialize the issyncable state to 1 for that sync adapter for each Account: is added.
android:syncadaptersettingsaction Specifies an action that can set the activity to synchronize.
defaults to null and if supplied it specifies a Intent action of an activity that can is used to adjust the sync adapter ' s sync settings. The activity must live in the same package as the sync adapter.
Syncadapter.java
SyncAdapter is inherited from the abstract class Abstractthreadedsyncadapter, which implements the method in Abstractthreadedsyncadapter, as follows:
@Override
public void Onperformsync (account account, Bundle Extras, String Authority,
Contentproviderclient provider, Syncresult Syncresult) {
TODO for synchronous operation
}
Abstractthreadedsyncadapter internally provides Startsync () and Cancelsync () two methods, and two methods are mainly called by the remote system process. Startsync () will start a thread by calling the
Abstractthreadedsyncadapter Onperformsync (account, Bundle, String, Contentproviderclient, Syncresult) method to perform the synchronization operation. So the actions in the Onperformsync method above are
is executed in the new thread. Cancelsync () will interrupt the synchronization operation.