当前位置:操作系统 > 安卓/Android >>

基于Andoird 4.2.2的同步框架源代码学习——同步发起端

关键组件:
ContentResolver
ContentService
SyncManager
SyncManager.ActiveSyncContext
SyncManager.SyncOperation
SyncManager.SyncHandler
ContentResolver
外部的应用程序通过调用ContentResolve.requestSync()静态方法发起同步:


[java]
/**
 * @param account which account should be synced
 * @param authority which authority should be synced
 * @param extras any extras to pass to the SyncAdapter.
 */ 
public static void requestSync(Account account, String authority, Bundle extras) { 
    validateSyncExtrasBundle(extras); 
    try { 
        getContentService().requestSync(account, authority, extras); 
    } catch (RemoteException e) { 
    } 

    /**
     * @param account which account should be synced
     * @param authority which authority should be synced
     * @param extras any extras to pass to the SyncAdapter.
     */
    public static void requestSync(Account account, String authority, Bundle extras) {
        validateSyncExtrasBundle(extras);
        try {
            getContentService().requestSync(account, authority, extras);
        } catch (RemoteException e) {
        }
    }
方法接收三个参数:

- account:需要同步的帐号

- authority:需要进行同步的authority

- extras:需要传递给sync adapter的附加数据

在这里,getContentService()方法返回系统服务ContentService的代理对象,然后通过它远程调用ContentService.requestSync()。

 


ContentService
ContentService是Android的系统服务,它提供一系列数据同步及数据访问等相关的操作。它的行为在IContentService.aidl中描述。

这里,通过远程调用ContentService.requestSync()方法来启动针对指定帐号(account)的指定内容(authority)的同步:


[java]
public void requestSync(Account account, String authority, Bundle extras) { 
    ... 
    try { 
        SyncManager syncManager = getSyncManager(); 
        if (syncManager != null) { 
            syncManager.scheduleSync(account, userId, authority, extras, 0 /* no delay */, 
                    false /* onlyThoseWithUnkownSyncableState */); 
        } 
    } 
    ... 

    public void requestSync(Account account, String authority, Bundle extras) {
        ...
        try {
            SyncManager syncManager = getSyncManager();
            if (syncManager != null) {
                syncManager.scheduleSync(account, userId, authority, extras, 0 /* no delay */,
                        false /* onlyThoseWithUnkownSyncableState */);
            }
        }
        ...
    }
在这个方法中,会获取一个SyncManager类的实例。顾名思义,SyncManager管理与同步相关的处理。

 


SyncManager

[java]
public void scheduleSync(Account requestedAccount, int userId, String requestedAuthority, 
        Bundle extras, long delay, boolean onlyThoseWithUnkownSyncableState) { 
    ... 
    final boolean backgroundDataUsageAllowed = !mBootCompleted || 
            getConnectivityManager().getBackgroundDataSetting(); 
    ... 
     
    // 产生一个同步帐户列表。对于手动同步,列表中仅有一个AccountUser元素,它封装了需要同步的帐号以及对应的应用程序(userId)  
    AccountAndUser[] accounts; 
    if (requestedAccount != null && userId != UserHandle.USER_ALL) { 
        accounts = new AccountAndUser[] { new AccountAndUser(requestedAccount, userId) }; 
    } 
    ... 
    for (AccountAndUser account : accounts) { 
        // 在这里,会扫描系统中所有提供了sync adapter的service:根据intent filter  
        // 然后从得到service info中取得各自的authority。service info从对应服务的meta-data标签中指定的sync adapter描述文件中解析出来。  
        final HashSet<String> syncableAuthorities = new HashSet<String>(); 
        for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapter : 
                mSyncAdapters.getAllServices(account.userId)) { 
            syncableAuthorities.add(syncAdapter.type.authority); 
        } 
 
        ... 
 
        for (String authority : syncableAuthorities) { 
            // 检查帐户是否能够同步  
            int isSyncable = mSyncStorageEngine.getIsSyncable(account.account, account.userId, 
                    authority); 
            if (isSyncable == 0) { 
                continue; 
            } 
            final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo; 
            syncAdapterInfo = mSyncAdapters.getServiceInfo( 
                    SyncAdapterType.newKey(authority, account.account.type), account.userId); 
            ... 
 
            if (isSyncable < 0) { <

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,