How does one listen for progress from Android SyncAdapter?
Asked Answered
O

2

6

I recall reading about a broadcast receiver interface from the sync adapter or some ResultReceiver of sync progress events. Is there something built into the SyncAdapter pattern or is it home-grown?

Overtly answered 11/3, 2011 at 3:5 Comment(1)
I have used this to update a progress on the UI via the activity. As I do not time the sync adapter I use an indeterminate progress. I would look at your sync code to see how you could use it without it been a continous non stopping process e.g use a number of threads working in a thread group or and Executor service. You could also send extra params to the Receiver every couple of seconds/minutes e.g code intent.putExtra(SYNCING_STATUS_TIME, 30); codeDiscomfiture
D
5

What works:

The method suggested in a 2010 Google IO session, Developing Android REST client applications is to place columns into your ContentProvider as tags to indicate that a record is being fetched or placed or etc. This allows a per-row spinner (or other visual change) to be placed in your UI. You might do that through a custom CursorAdapter that drives a ListView. Your ContentProvider is on the hook to make the flags change as needed.

What doesn't:

You can also use a SyncStatusObserver -- Which is pretty much useless since it responds to every change of status, not just your specific account/contentauthority pair, and really doesn't tell you much anything at all other than that a change occured. So, you can't tell what is being synced, and you can't distinguish "start of sync event" from "end of sync event". Worthless. :P

Daugava answered 14/3, 2011 at 16:37 Comment(1)
I went to a pub-sub approach to which I use ResultReceiver to respond back to the UI from the sync thread. The service adds and removes subscribers while the sync thread is running and if any are available, it broadcasts to the active receivers. I was thinking of using the SyncStatusObserver to trigger to the UI that the periodic sync has triggered -- i would proxy it into a "start-sync" subscription status-event.Overtly
D
11

I Have just implemented a Broadcast receiver from a sync adapter and it works like clockwork!

Using a Receiver set as an inner class and calling registerReceiver in onCreate and unregisterReceiver in onDestroy did this for me.

As I have one strategy method to spawn and query a number of threads, All I have at the begining of a SyncAdapter run are:

Intent intent = new Intent();
intent.setAction(ACTION);
intent.putExtra(SYNCING_STATUS, RUNNING);
context.sendBroadcast(intent); 

And at the end of the sync run i have:

intent.putExtra(SYNCING_STATUS, STOPPING);
context.sendBroadcast(intent); 

Within my Activity, I declare:

onCreate(Bundle savedInstance){

super.onCreate(savedInstance);
SyncReceiver myReceiver = new SyncReceiver();
RegisterReceiver(myReceiver,ACTION);

}



onDestroy(){

super.onPause();
unRegisterReceiver(myReceiver);

}



 public class SyncReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
            Bundle extras = intent.getExtras();
    if (extras != null) {
        //do something  
    }
   }
 }

For this scenario you do not need to add your receiver to the manifest file. just use as is!

Discomfiture answered 17/4, 2014 at 14:29 Comment(5)
Mail This is interesting and probably very accurate, but the question was about listening (monitoring) progress. The issue was that my sync adapter was running and running, with no end in sight, so I wanted to provide progress to the UI.Overtly
I have used this to update a progress on the UI via the activity. As I do not time the sync adapter I use an indeterminate progress. I would look at your sync code to see how you could use it without it been a continous non stopping process e.g use a number of threads working in a thread group or and Executor serviceDiscomfiture
This is the best answer if you are looking for a way to manipulate the start and end of the SyncAdapter.Tendinous
A simple extension for giving user progress info is to add another broadcast extra intent.putExtra(SYNCING_STATUS_PROGRESS, ITEMS_PROCESSED_PRECENT);. Then when Activity sees SYNC_START show intermediate progress spinner. When Activity sees SYNCING_STATUS_PROGRESS show progress bar.Underskirt
While this works for most cases, it won't handle the case that the Activity is launched while a SyncAdapter is already running. You will not receive the initial broadcast. (And if you're thinking of then creating a receiver on the SyncAdapter side to respond to status requests, the trouble there is that the it runs in the same thread as the adapter itself and won't respond on time.)Quizmaster
D
5

What works:

The method suggested in a 2010 Google IO session, Developing Android REST client applications is to place columns into your ContentProvider as tags to indicate that a record is being fetched or placed or etc. This allows a per-row spinner (or other visual change) to be placed in your UI. You might do that through a custom CursorAdapter that drives a ListView. Your ContentProvider is on the hook to make the flags change as needed.

What doesn't:

You can also use a SyncStatusObserver -- Which is pretty much useless since it responds to every change of status, not just your specific account/contentauthority pair, and really doesn't tell you much anything at all other than that a change occured. So, you can't tell what is being synced, and you can't distinguish "start of sync event" from "end of sync event". Worthless. :P

Daugava answered 14/3, 2011 at 16:37 Comment(1)
I went to a pub-sub approach to which I use ResultReceiver to respond back to the UI from the sync thread. The service adds and removes subscribers while the sync thread is running and if any are available, it broadcasts to the active receivers. I was thinking of using the SyncStatusObserver to trigger to the UI that the periodic sync has triggered -- i would proxy it into a "start-sync" subscription status-event.Overtly

© 2022 - 2024 — McMap. All rights reserved.