How to know when sync is finished?

后端 未结 3 1874
南旧
南旧 2020-12-04 17:46

I have implemented a sync adapter and I want to get a callback when it finishes in my activity. I have tried using ContentResolver.addStatusChangeListener, but I am only get

3条回答
  •  感情败类
    2020-12-04 18:33

    One solution that I have found is to ditch the ContentResolver completely, and implement my own broadcast. Basically, add this in the sync adapter, at the end of onPerformSync:

    Intent i = new Intent(SYNC_FINISHED);
    sendBroadcast(i);
    

    And this in the activity:

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(syncFinishedReceiver, new IntentFilter(DataSyncService.SYNC_FINISHED));
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(syncFinishedReceiver);
    }
    
    private BroadcastReceiver syncFinishedReceiver = new BroadcastReceiver() {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(Const.TAG, "Sync finished, should refresh nao!!");
        }
    };
    

    This seems to work just fine, however I was hoping to find something in the SDK that directly notify me when a sync is finished.

提交回复
热议问题