2015-09-09 44 views
0

我的同步適配器存在很大問題。 第一步,我想每次調用同步,一個特殊的活動恢復(非常低效,我知道但它只能用於測試)。 當我在啓動應用程序(從Android Studio安裝,用戶數據可用)後第一次打電話給我的ContentResolver.requestSync時,它會調用我的onPerformSync方法。 但後來當我打電話給requestSync時,onPerformSync方法從未被調用過。這就是我所說的每一次觸發同步代碼:SyncAdapter只能同步一次

public void updateContent(Activity activity) { 
    final Account account = APIHelper.getInstance().getAccount(); 
    final Bundle bundle = new Bundle(); 
    bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); 
    bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true); 


    Context context = MyApp.getApplication().getApplicationContext(); 
    final AccountManager manager = AccountManager.get(context); 
    if (account == null) { 
     manager.addAccount(Constants.ACCOUNT_TYPE, "", null, null, activity, new AccountManagerCallback<Bundle>() { 
      @Override 
      public void run(AccountManagerFuture<Bundle> future) { 
       try { 
        Bundle bnd = future.getResult(); 
        final Account account1 = APIHelper.getInstance().getAccount(); 
        ContentResolver.requestSync(account1, Constants.AUTH_PROVIDER_NAME, bundle); 
       } catch (Exception e) { 
        Log.e(TAG, e.getLocalizedMessage()); 
       } 
      } 
     }, null); 

    } else { 
     ContentResolver.requestSync(account, Constants.AUTH_PROVIDER_NAME, bundle); 

    } 
} 

當我檢查在Android的帳戶設置,它seemes的同步正在進行中(動畫同步符號被打開,它體現在「同步。進步」 我也注意到this後,試圖在我的代碼添加這一點,但沒有效果:

ContentResolver.setSyncAutomatically(account, getString(R.string.CONTENT_AUTHORITY), true); 
ContentResolver.setIsSyncable(account, getString(R.string.CONTENT_AUTHORITY), 1); 

我試圖把它修改到同步每分鐘一個週期同步,但是這也並不工作。

回答

0

我終於解決了這個問題:在我的SyncAdapter中,我試圖向用戶界面發送Toast。我通過一個處理程序做了這個。這似乎打破了SyncAdapter。當我刪除這個邏輯時,它就起作用了。