Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one listen for progress from Android SyncAdapter?

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?

like image 909
mobibob Avatar asked Mar 11 '11 03:03

mobibob


2 Answers

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!

like image 180
Lettings Mall Avatar answered Nov 10 '22 13:11

Lettings Mall


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

like image 33
jcwenger Avatar answered Nov 10 '22 13:11

jcwenger