上一篇博文我先介紹了賬號與同步的賬號管理,這篇就介紹一下還有一部分。就是android給提供的sync同步機制的使用。
事實上sync機制的使用和上一篇博文中介紹的賬號管理非常類似,也是基於binder機制的跨進程通信。首先它須要一個Service。這個服務提供一個Action給系統以便系統能找到它。然后就是繼承和實現AbstractThreadedSyncAdapter。此類中包括實現了ISyncAdapter.Stub內部類。這個內部類封裝了遠程接口調用,這個類getSyncAdapterBinder()方法,返回內部類的IBinder形式,以便對AbstractThreadedSyncAdapte進行遠程調用;在manifest中須要對Service注冊,並且指定meta-data。這個meta-data是一個xml文件,在SampleSyncAdapter實例中,它的名字是syncadapter.xml,這個文件指定了賬號和被監聽的contentprovider。
以下分別介紹這幾個文件:
SyncService.java
SyncService是一個繼承普通Service的服務,用來給遠端進程提供服務,在onBind方法中返回IBinder。
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();
}
}
它的action取值為android.content.SyncAdapter。注冊例如以下:
<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>
一個適配器僅僅能同步一個Authority,若想使一個賬戶同步多個Authority,能夠向系統注冊多個綁定同一賬戶的sync-adapter。
syncadapter.xml
syncadapter.xml文件指定了此Service所監聽的contentprovider的Authority,還指定了監聽此Authority的賬號類型accountType,這個賬號類型和上一篇文章中的賬號類型是相關的。
<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 指定要同步的ContentProvider在其AndroidManifest.xml文件里有個android:authorities屬性。
android:accountType 表示進行同步的賬號的類型。
attributes indicate which content authority and for which account types this sync adapter serves.
android:userVisible 設置是否在“設置”中顯示
defaults to true and controls whether or not this sync adapter shows up in the Sync Settings screen.
android:supportsUploading 設置是否必須notifyChange通知才干同步
defaults to true and if true an upload-only sync will be requested for all syncadapters associated with an authority whenever that authority's content provider does a notifyChange(android.net.Uri, android.database.ContentObserver, boolean) with syncToNetwork set to true.
android:allowParallelSyncs 是否支持多賬號同一時候同步
defaults to false and if true indicates that the sync adapter can handle syncs for multiple accounts at the same time. Otherwise the SyncManager will wait until the sync adapter is not in use before requesting that it sync an account's data.
android:isAlwaysSyncable 設置全部賬號的isSyncable為1
defaults to false and if true tells the SyncManager to intialize the isSyncable state to 1 for that sync adapter for each account that is added.
android:syncAdapterSettingsAction 指定一個能夠設置同步的activity的Action。
defaults to null and if supplied it specifies an Intent action of an activity that can be used to adjust the sync adapter's sync settings. The activity must live in the same package as the sync adapter.
SyncAdapter.java
SyncAdapter是繼承自抽象類AbstractThreadedSyncAdapter的。它實現了AbstractThreadedSyncAdapter中的方法,例如以下:
@Override
public void onPerformSync(Account account, Bundle extras, String authority,
ContentProviderClient provider, SyncResult syncResult) {
//TODO 進行同步操作
}
AbstractThreadedSyncAdapter內部提供startSync()和cancelSync()兩個方法。兩個方法主要是被遠端系統進程調用。
startSync()將會啟動一個線程,通過在該線程中調用
AbstractThreadedSyncAdapter的 onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult) 方法來運行同步操作。所以上面onPerformSync方法中的操作都
是在新線程中運行的。cancelSync()將會中斷同步操作。