Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8774405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:33:36+00:00 2026-06-13T18:33:36+00:00

I want to show a ProgressBar in the ActionBar while my SyncAdapter is actively

  • 0

I want to show a ProgressBar in the ActionBar while my SyncAdapter is actively synchronizing content to and from the web.

I have tried using the SyncStatusObserver together with ContentProvider.addStatusChangeListener. However, I cannot check if a SyncAdapter is actively running. I can only check:

  1. SyncAdapter is pending using ContentResolver.isSyncPending
  2. SyncAdapter is pending OR actively working using ContentResolver.isSyncActive

These flags can be combined: !isSyncPending && isSyncActive so that it is possible to check that a SyncAdapter is actively working and does not have any pending work. However, in some cases the SyncAdapter is actively working AND have a second pending request waiting for it.

It seems so simple but I can’t find a way around this problem. Having the ProgressBar visible when the SyncAdapter is not running is giving users the impression that the synchronization is very slow. Having it not show the ProgressBar makes the user think nothing is happening.

The above solution in code is shown below. We register the observer in the activity.onResume:

 int mask = ContentResolver.SYNC_OBSERVER_TYPE_PENDING | ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE;
 syncHandle = ContentResolver.addStatusChangeListener(mask, syncObserver);

The syncObserver is here defined as:

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:33:37+00:00Added an answer on June 13, 2026 at 6:33 pm

    I finally found a solution to the problem. The idea is to use the ContentResolver’s getCurrentSyncs() or getCurrentSync() methods, whichever is available. The methods below will check if a sync operation is currently working for an account and authority. It requires API level 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;
    }
    

    An Activity then registers for updates in onResume() and unregisters in onDestroy(). Also, one have to update state manually in onResume() to catch up with current status.

    Here is an implementation that does just that. Subclasses should themselves define

    • what account to use (implementing getAccount())
    • what authoritity to use (the field CONTENT_AUTHORITY)
    • how to display sychronization status (implementing updateState(boolean isSynchronizing))

    I hope it will help someone in the future.

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to show a progress bar while downloading a file from the web
I want to show the content of a nested table using a cursor. I've
I have a search function (using a web service) which takes a while to
I'm using Drupal 7 and Views module in my site. And want show number
I want to show a border type look for an element but without using
I have a WebView that is loading a page from the Internet. I want
I want to show my application user a progressbar for following actions: when they
I have two Date objects. EndDate and StartDate. I want to show difference(including hours
I want to use progress bar to show file upload progress. I'm currently using
I have a ProgressBar. It works ok but I need to show the max

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.