SyncAdapter运行动画 - 如何知道SyncAdapter是否正在主动同步

时间:2012-10-16 12:40:11

标签: android android-contentresolver android-syncadapter

我想在ActionBar中显示ProgressBar,而我的SyncAdapter正在主动与网络同步内容。

我尝试将SyncStatusObserverContentProvider.addStatusChangeListener一起使用。但是,我无法检查SyncAdapter是否正在运行。我只能检查:

  1. 使用ContentResolver.isSyncPending
  2. 挂起SyncAdapter
  3. SyncAdapter待处理或正在使用ContentResolver.isSyncActive
  4. 正在使用

    可以组合使用这些标志:!isSyncPending && isSyncActive,以便可以检查SyncAdapter是否正在正常工作,并且没有任何待处理的工作。但是,在某些情况下,SyncAdapter正在正常工作,并有第二个等待它的待处理请求。

    看起来很简单,但我无法找到解决这个问题的方法。在SyncAdapter未运行时显示ProgressBar会给用户一种同步非常慢的印象。让它不显示ProgressBar会让用户认为没有发生任何事情。

    上面的代码解决方案如下所示。我们在activity.onResume:

    中注册观察者
     int mask = ContentResolver.SYNC_OBSERVER_TYPE_PENDING | ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE;
     syncHandle = ContentResolver.addStatusChangeListener(mask, syncObserver);
    

    syncObserver在此定义为:

    syncObserver = new SyncStatusObserver()
    {
        @Override
        public void onStatusChanged(int which)
        {
            Account account = getSomeAccount();
            boolean syncActive = ContentResolver.isSyncActive(account, CONTENT_AUTHORITY);
            boolean syncPending = ContentResolver.isSyncPending(account, CONTENT_AUTHORITY);
            boolean isSynchronizing = syncActive && !syncPending;
            updateRefreshButtonState();
        }
    }
    

1 个答案:

答案 0 :(得分:16)

我终于找到了问题的解决方案。我们的想法是使用ContentResolver的getCurrentSyncs()getCurrentSync()方法,无论哪种方法都可用。以下方法将检查同步操作当前是否适用于帐户和权限。它需要API级别8(Froyo = Android 2.2)。

private static boolean isSyncActive(Account account, String authority)
{
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    {
        return isSyncActiveHoneycomb(account, authority);
    } else
    {
        SyncInfo currentSync = ContentResolver.getCurrentSync();
        return currentSync != null && currentSync.account.equals(account) &&
               currentSync.authority.equals(authority);
    }
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private static boolean isSyncActiveHoneycomb(Account account, String authority)
{
    for(SyncInfo syncInfo : ContentResolver.getCurrentSyncs())
    {
        if(syncInfo.account.equals(account) &&
           syncInfo.authority.equals(authority))
        {
            return true;
        }
    }
    return false;
}

然后,活动会在onResume()中注册更新,并在onDestroy()中注册注册。此外,还必须在onResume()中手动更新状态以赶上当前状态。

这是一个实现这一点的实现。子类本身应该定义

  • 使用什么帐户(实施getAccount()
  • 使用什么授权(字段CONTENT_AUTHORITY
  • 如何显示同步状态(实施updateState(boolean isSynchronizing)

我希望将来可以帮助某人。

import android.accounts.Account;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.SyncInfo;
import android.content.SyncStatusObserver;
import android.os.Build;
import android.os.Bundle;

public abstract class SyncActivity extends Activity
{
    private static final String CONTENT_AUTHORITY = "com.example.authority";
    private Object syncHandle;
    private SyncStatusObserver observer;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        observer = new SyncStatusObserver()
        {
            @Override
            public void onStatusChanged(int which)
            {
                runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        Account account = getAccount();
                        boolean isSynchronizing =
                                isSyncActive(account, CONTENT_AUTHORITY);
                        updateState(isSynchronizing);
                    }
                });
            }
        };
    }

    @Override
    protected void onResume()
    {
        super.onResume();

        // Refresh synchronization status
        observer.onStatusChanged(0);

        // Watch for synchronization status changes
        final int mask = ContentResolver.SYNC_OBSERVER_TYPE_PENDING |
                ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE;
        syncHandle = ContentResolver.addStatusChangeListener(mask, observer);
    }

    @Override
    protected void onPause()
    {
        super.onPause();

        // Remove our synchronization listener if registered
        if (syncHandle != null)
        {
            ContentResolver.removeStatusChangeListener(syncHandle);
            syncHandle = null;
        }
    }

    private static boolean isSyncActive(Account account, String authority)
    {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        {
            return isSyncActiveHoneycomb(account, authority);
        } else
        {
            SyncInfo currentSync = ContentResolver.getCurrentSync();
            return currentSync != null && currentSync.account.equals(account) 
                    && currentSync.authority.equals(authority);
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private static boolean isSyncActiveHoneycomb(Account account,
                                                         String authority)
    {
        for(SyncInfo syncInfo : ContentResolver.getCurrentSyncs())
        {
            if(syncInfo.account.equals(account) &&
                    syncInfo.authority.equals(authority))
            {
                return true;
            }
        }
        return false;
    }

    protected abstract Account getAccount();
    protected abstract void updateState(boolean isSynchronizing);
}