我試圖使用AbstractThreadedSyncAdapter將我的本地數據庫與我的服務器同步。我遵循我找到的指南,但我無法正常工作。它適用於Android 2.2。使用AbstractThreadedSyncAdapter同步Android數據庫:不調用onPerformSync
我有我的Authenticator系統工作。在我AccountAuthenticatorActivity,創建帳戶後,我做的:
Bundle params = new Bundle();
params.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
params.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false);
params.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
ContentResolver.addPeriodicSync(account, "com.syncal.authority", params, 30);
ContentResolver.setSyncAutomatically(account, "com.syncal.authority", true);
ContentResolver.requestSync(account,"com.syncal.authority",params);
創建我SyncService和我宣佈它在我的清單。一些功能:
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "inBind() " + intent.getAction());
return getSyncAdapter().getSyncAdapterBinder();
}
private synchronized AbstractThreadedSyncAdapter getSyncAdapter() {
Log.i(TAG, "getSyncAdapter()");
if (mSyncAdapter == null) {
mSyncAdapter = new SweetSyncAdapter(this, true);
}
return mSyncAdapter;
}
然後我實現了我的SyncAdapter。 onPerformSync函數是空的,我只是想知道它是否被調用,並且從來沒有調用。
public class SweetSyncAdapter extends AbstractThreadedSyncAdapter {
Context mContext;
private String AUTH_TOKEN_TYPE;
private AccountManager mAccountManager;
private String mAuthToken;
private final String TAG = "SweetContactSync";
static final String LAST_SYNC_KEY = "lastSync";
public SweetSyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
Log.i(TAG, "SweetSyncAdapter");
mContext = context;
AUTH_TOKEN_TYPE = mContext.getString(R.string.account_type);
mAccountManager = AccountManager.get(mContext);
}
public interface ISugarRunnable {
public void run() throws URISyntaxException, OperationCanceledException, AuthenticatorException, IOException,
AuthenticationException;
}
class SugarRunnable implements Runnable {
ISugarRunnable r;
Account mAccount;
SyncResult mSyncResult;
public SugarRunnable(Account acc, SyncResult syncResult, ISugarRunnable _r) {
r = _r;
mAccount = acc;
mSyncResult = syncResult;
}
public void run() {
try {
r.run();
} catch ... //All the catchs
}
}
@Override
public void onPerformSync(final Account account, Bundle extras, String authority,
ContentProviderClient provider, SyncResult syncResult) {
Log.i(TAG, "onPerformSync()");
}
}
我的XML/sync_adapter.xml具有下面的代碼:
<?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:contentAuthority="com.syncal.authority"
android:accountType="com.syncal.account"
/>
這裏是清單的服務部分:
<service android:name=".sweet.SweetSyncService" android:exported="true"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data android:name="android.content.SyncAdapter"
android:resource="@xml/sync_adapter" />
</service>
我甚至創建了自己的ContentProvider(該功能空的,他們只是寫在日誌中),因爲我跟隨的指南說它是需要的,我也在清單中聲明它,但是沒有一個函數被調用。
有人知道爲什麼我的onPerformSync函數沒有被調用嗎?我的SweetSyncService的onBind函數被調用,它創建了我的SweetSyncAdapter,但我的日誌沒有顯示任何消息。
感謝您的閱讀,我知道我輸入了很多代碼,但是我一直在研究它一段時間,但我無法解決它。
能否提供本指南的鏈接? :) –
爲什麼你存儲上下文(可能的內存泄漏!),而你的超類爲你做這件事http://developer.android.com/reference/android/content/AbstractThreadedSyncAdapter.html#getContext%28%29(仍然有可能內存泄漏!)? –