我正在尝试使用AbstractThreadedSyncAdapter将我的本地数据库与我的服务器同步。我按照我发现的指南,但我不能让它工作。它适用于Android 2.2。
我的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,但我的日志没有显示任何消息。
感谢您的阅读,我知道我输入了很多代码,但我已经有一段时间了,我无法解决它。
答案 0 :(得分:2)
我终于找到了错误。我在清单中添加了一些权限,现在可以使用了:
<uses-permission android:name="android.permission.READ_SYNC_STATS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />