How does one listen for progress from Android SyncAdapter?

后端 未结 2 1550
傲寒
傲寒 2020-12-10 07:30

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 pa

相关标签:
2条回答
  • 2020-12-10 08:11

    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

    0 讨论(0)
  • 2020-12-10 08:19

    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!

    0 讨论(0)
提交回复
热议问题