How does one listen for progress from Android SyncAdapter?

强颜欢笑 提交于 2019-11-27 03:32:22

问题


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?


回答1:


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




回答2:


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!



来源:https://stackoverflow.com/questions/5268536/how-does-one-listen-for-progress-from-android-syncadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!