5

我正在開發一個Android應用程序,它與服務器執行後臺同步(使用SyncAdapter和認證等)。有沒有人在Android SyncObserver上實現(或獲得更多信息)

當前臺應用程序(帶UI)啓動時,可能正在進行背景同步,或者可以選擇通過UI按鈕啓動它。

我想要一種「插入」正在進行的後臺同步(無論是由系統啓動,還是定期同步設置或用戶界面),並顯示它在前臺活動中的進度。

ContentResolver文檔(http://developer.android.com/reference/android/content/ContentResolver.html)提到了一個神祕的「SyncObserver」,它沒有鏈接到javadoc,沒有記錄(我可以找到) 。

還有一些其他網頁提到它(http://www.chinaup.org/docs/migrating/m5-0.9/changes/android.content.ContentResolver.html)但我找不到更多關於它。

有沒有人實現過這個野獸?

如果沒有,有沒有人有跟蹤前臺活動中的後臺同步進度的示例代碼或建議?

回答

3

我遇到了同樣的問題,最終實現了1)來自SyncAdapter的廣播,2)使用SharedPreferences來指示狀態。

在SyncAdapter,像這樣的:

public static final String START_SYNC = "com.whatever.sync.start"; 
public static final String STOP_SYNC = "com.whatever.sync.stop"; 
public static final String SYNC_PROGRESS = "syncProgress"; 


public void onPerformSync(Account account, Bundle extras, String authority, 
    ContentProviderClient provider, SyncResult syncResult) { 

     // Add an integer to the shared settings to indicate the status 
     SharedPreferences settings = mContext.getSharedPreferences(Constants.PREFS, 0); 
     SharedPreferences.Editor editor = settings.edit(); 
     editor.putInt(SyncAdapter.SYNC_PROGRESS, 0); 
     editor.commit(); 

     Intent intent = new Intent(); 
     intent.setAction(START_SYNC); 
     mContext.sendBroadcast(intent); 


     //... do some stuff, setting SYNC_PROGRESS to other values and 
     // sending more broadcasts as the state changes 

     // When we are done, remove the "in progress" setting and store some 
     // other data 
     editor.putString(SyncAdapter.LAST_UPDATED, new Date().toString()); 
     editor.remove(SyncAdapter.SYNC_PROGRESS); 
     editor.commit(); 

     Intent stopIntent = new Intent(); 
     stopIntent.setAction(STOP_SYNC); 
     mContext.sendBroadcast(stopIntent); 
     } 

在活動中,我們做兩件事情在簡歷1)檢查共享偏好同步是否正在進行中,2)註冊偵聽用接收器廣播。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
// .. do some UI stuff 

    mReceiver = new SyncReceiver(this); 
} 

@Override 
public void onResume() { 
    super.onResume(); 

    IntentFilter intentFilter = new IntentFilter(); 
    intentFilter.addAction(SyncAdapter.START_SYNC); 
    intentFilter.addAction(SyncAdapter.STOP_SYNC); 
    registerReceiver(mReceiver, intentFilter); 

    showProgress(); 
} 

public void showProgress() { 
    SharedPreferences settings = getSharedPreferences(Constants.PREFS, 0); 
    if (settings.contains(SyncAdapter.SYNC_PROGRESS)) { 
     // ... set the UI to show that a sync is in progress 
    } else { 
     // ... set the UI to show that a sync is NOT in progress 
    } 
} 

private class SyncReceiver extends BroadcastReceiver { 

    private MyActivity mActivity; 

    public SyncReceiver(MyActivity activity) { 
     mActivity = activity; 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals(SyncAdapter.START_SYNC)) { 
      Log.i("@string/app_name", "Started sync"); 
      mActivity.showProgress(); 
     } 
     else if (intent.getAction().equals(SyncAdapter.STOP_SYNC)) { 
      Log.i("@string/app_name", "Started sync"); 
      mActivity.showProgress(); 
     } 
    } 
} 

這似乎適用於我。我必須承認,由於廣播的異步性,我感覺有一些潛在的問題。任何關於改進我的方法的意見,將不勝感激!

+0

我覺得這個唯一的問題,就是因爲你暗示,涉及到後臺同步的異步性質。 – 2011-06-06 14:32:43

+0

我不知道ContentResolver.isSyncActive(),這可能是不是檢查sharedpreference更好。謝謝。 – fredw 2011-06-06 17:47:14

5

感謝您的回答。

由於背景的異步性質同步您的應用程序(活動)可以與後臺同步已經開始,你與存儲在偏好狀態檢測開始。

我所做的是實現一個SyncObserver類,該類實現SyncStatusObserver接口並在掛起/恢復時創建/銷燬。

syncObserverHandle = ContentResolver.addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE, new SyncObserver()); 

這被通知同步有關的任何活動(待定,開工等),我也使用

ContentResolver.isSyncActive(); 

Android的API來這是非常基本的檢查情況,你必須尊重規則,關於什麼是UI線程上完成的,哪些不是,但如果有人想看看我的實現只是張貼問題,並指出了我吧,我會高興地回答。

+1

我想帶你上你的實物:)你的解決方案聽起來像它可能解決我的問題http://stackoverflow.com/questions/9268453/android-syncadapter-callback – JosephL 2012-03-08 22:30:34

+0

@Andrew Mackenzie我也喜歡看到你的實現,請回答上面給出的問題由JosephL – Thomas 2015-07-15 09:45:01

+0

@Thomas你應該看看[BasicSyncAdapter]的代碼(https://developer.android.com/samples/BasicSyncAdapter/src/com.example.android.basicsyncadapter/EntryListFragment。 html#l82)from android google doc。特別是EntryListFragment的代碼。 – hermannovich 2015-10-08 14:08:04

相關問題