I first introduced the account and the synchronization of account management, this article on the introduction of a part. Is the use of the sync synchronization mechanism provided by Android.
In fact, the use of the sync mechanism is very similar to the account management described in the previous blog post, and is based on the binder mechanism of cross-process communication. First it needs a service. This service provides an action to the system so that the system can find it. Then there is the inheritance and implementation of Abstractthreadedsyncadapter. This class includes the implementation of the isyncadapter.stub inner class. This inner class encapsulates the remote interface call, the class Getsyncadapterbinder () method, which returns the IBinder form of the inner class for remote invocation of ABSTRACTTHREADEDSYNCADAPTE The service register is required in the manifest, and the meta-data is specified. This meta-data is an XML file, in the Samplesyncadapter instance, whose name is Syncadapter.xml, which specifies the account number and the contentprovider being monitored.
These documents 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. For example, the following:
<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 with one account, you can 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 Setting whether you must notifychange notify talent to sync
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 multiple accounts sync at the same time
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. It implements the methods in Abstractthreadedsyncadapter, such as the following:
@Override
public void Onperformsync (account account, Bundle Extras, String Authority,
Contentproviderclient provider, Syncresult Syncresult) {
TODO for synchronous operation
}
Abstractthreadedsyncadapter provides two methods of Startsync () and Cancelsync (). 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 run the synchronization operation. So the actions in the Onperformsync method above are
Is running in a new thread. Cancelsync () will interrupt the synchronization operation.
Synchronization of Android account and synchronization